0

Can anyone help me please? There are 2 queries. How join them to 1 query?

$sql1="UPDATE gallery 
       SET namesk='$_POST[namesk]', nameen='$_POST[nameen]', descriptionsk='$_POST[descriptionsk]', descriptionen='$_POST[descriptionen]', date='$_POST[date]', url1='$_POST[url1]' 
       WHERE namesk='$_GET[namesk]'";

$sql2="UPDATE photos 
       SET namesk_gallery='$_POST[namesk]'
       WHERE namesk_gallery='$_GET[namesk]'";

They works, but I would like to know, how to create one query. Thanks very much.

Tomi
  • 5
  • 5

1 Answers1

1

Its easy to do (I use mysqli functionality for this):

$conn = mysqli_init();
$conn->real_connect("hostname", "username", "password", "dbname");

$query = "
    UPDATE gallery 
    SET namesk='{$_POST['namesk']}', nameen='{$_POST['nameen']}', descriptionsk='{$_POST['descriptionsk']}', descriptionen='{$_POST['descriptionen']}', date='{$_POST['date']}', url1='$_POST[url1]' 
    WHERE namesk='{$_GET['namesk']}';

    UPDATE photos 
    SET namesk_gallery='{$_POST['namesk']}'
    WHERE namesk_gallery='{$_GET['namesk']}';
";

$result = $conn->multi_query($query);

But make sure that after each of your queries you use the semi-colon (;) to separate them.

Edits

Added variable encapsulation for the $_POST and $_GET (don't know how you are using both at once...)

This answer gives a full one query version, I'd reproduce but that seems a waste when the answer is just there and with a very good explanation

Community
  • 1
  • 1
Can O' Spam
  • 2,718
  • 4
  • 19
  • 45
  • In table "gallery" is 'namesk' type unique. In table "photos" is not 'namesk_gallery' unique, and it's there a many times. When I used one query with function JOIN, that cause, only one row in 'photos.namesk_gallery' was changed, not everyone with same name from $_GET['namesk']. ...All fields in the form are required, therefore I didn't use $var=isset... I use this code for changing informations about files in database from Form in admin environment. $_GET I got from button "edit" from another file.php. – Tomi Nov 20 '15 at 14:13
  • `JOIN` is designed for `SELECT` queries, using `JOIN` in an `UPDATE` will cause errors and the query to fail, uniques don't really matter with `UPDATE` unless you are setting the field to be something... – Can O' Spam Nov 20 '15 at 14:18