0
$schoolinfo = mysqli_prepare($con, "UPDATE table SET firstname=?, lastname=? from school where   foreignkey='$id'");

mysqli_stmt_bind_param($schoolinfo,'ss', $firstname, $lastname)


for ($i=0;$i<count($_POST['row']);$i++){     
$firstname = mysqli_real_escape_string($con, $_POST['firstname'][$i]);
$lastname = mysqli_real_escape_string($con, $_POST['lastname'][$i]);
mysqli_stmt_execute($schoolinfo);
}

This updates all rows with the same firstname and lastname.

I want to update rows from selection where foreignkey = '$id' and rownumber ='i'

Any queries or subqueries out there?

4
  • You should not use mysqli_real_escape_string when using bind_param. You'll end up escaping twice. Commented Jan 1, 2014 at 0:51
  • You're repeatedly updating the same row, there's no rownumber in your query. Commented Jan 1, 2014 at 0:56
  • @Barmar i just exactly edited it barmar same issue Commented Jan 1, 2014 at 0:58
  • Foreign keys only, no Ids. Table has multiple rows with same foreign key. I want to post the info from array into the correct rows. Commented Jan 1, 2014 at 1:09

2 Answers 2

1

just remove this from school from your update query and give a name to your table .

like that

 $schoolinfo = mysqli_prepare($con, "UPDATE school SET firstname=?, lastname=? where   foreignkey='$id'");

you mixed between SELECT and UPDATE.

i dont know if im wrong or , you have binded firstname and lastname before the loop .

try this

$schoolinfo = mysqli_prepare($con, "UPDATE school SET firstname=?, lastname=? WHERE foreignkey='$id'");

for ($i=0;$i<count($_POST['row']);$i++){     
$firstname = $_POST['firstname'][$i];
$lastname = $_POST['lastname'][$i];
mysqli_stmt_bind_param($schoolinfo,'ss', $firstname, $lastname)
mysqli_stmt_execute($schoolinfo);
}
Sign up to request clarification or add additional context in comments.

1 Comment

You don't need to bind in the loop. bind_param uses references.
0

Try this:

$schoolinfo = mysqli_prepare($con, "UPDATE school SET firstname=?, lastname=?
                                    WHERE foreignkey=? and rownumber = ?");

mysqli_stmt_bind_param($schoolinfo,'sssi', $firstname, $lastname, $id, $i);

for ($i=0;$i<count($_POST['row']);$i++){     
    $firstname = $_POST['firstname'][$i];
    $lastname = $_POST['lastname'][$i];
    mysqli_stmt_execute($schoolinfo);
}

You didn't have rownumber in the query. And it's best to use bind_param for all variables that you're substituting.

4 Comments

I need to create the rownumber it doesn't exist in the table
I am pretty sure I have to use a SELECT subquery here to create the row_number()
If you search SO for [mysql] rownumber you will find examples of that. But you need an ordering criteria for your table, otherwise the row number assignments will be random.
I'm a little confused, though. Isn't the id field a unique identifier? How can there be multiple rows with the same id?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.