简体   繁体   中英

php (array)-> mysql (multiple rows with a foreign key) Update specific row in table where id and row number varies

$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?

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);
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM