update existing row in mysql using php coding

*爱你&永不变心* 提交于 2019-12-12 05:29:50

问题


in my db i have 20+ columns. i added 19 columns throught input form and stored in db succesfully. i fetch few rows from db in my main page. in my main page 1 more column is there. that is status column, it is a combo box type. if i click status column it should show 4 values. i want to select one of the values and then when i click save button it must go to stored in db with that same ID. how to do that? i tried but its not updated in mysql db...

mainpage combo box coding:

echo "\t<td><form action=statusdb.php method=post>
<select name=update><option value=empty></option><option value=Confirm>Confirm</option><option value=Processing>Processing</option><option value=Pending>Pending</option><option value=Cancelled>Cancelled</option></select>
<input name=\"update[".$a_row['slno']."]\"; type=submit id=id value=Save></form>
</td>\n";

status db coding:

if (isset($_GET['id']))
{ 
$id = mysql_real_escape_string($_GET['id']);
$sql = mysql_query("UPDATE guestdetails SET status = '" . $_POST['update'] ."'");
if(!$sql)
{
    die("Error" .mysql_error());
}
}

help me how to do that?


回答1:


In your IF should be $_POST, not $_GET Also, need to add WHERE clause, like this:

if (isset($_POST['id']))
{ 
  $id = mysql_real_escape_string($_POST['id']);
  $update= mysql_real_escape_string($_POST['update']);

  $sql = mysql_query("UPDATE guestdetails SET status = '$update' WHERE id='$id'");
if(!$sql)
{
    die("Error" .mysql_error());
}
}

Also, you used name update twice, once in <select> once in <input>, take that one from <input> out, make it hidden field with name id and value of your slno row:

echo "\t<td>
  <form action=statusdb.php method=post>
    <select name=update>
      <option value=empty></option>
      <option value=Confirm>Confirm</option>
      <option value=Processing>Processing</option>
      <option value=Pending>Pending</option>
      <option value=Cancelled>Cancelled</option>
    </select>
    <input name='id' type='hidden' value='".$a_row['slno']."';>
    <input type='submit'>Save</button>
  </form>
</td>\n";



回答2:


Try like below:

<form action="statusdb.php" method="post">
<?php
while($a_row = mysql_fetch_array($sql)) {
    $sl_no = $a_row['slno'];
    echo '<select name="update['.$sl_no.']"><option value=empty></option><option value=Confirm>Confirm</option><option value=Processing>Processing</option><option value=Pending>Pending</option><option value=Cancelled>Cancelled</option></select> '; 
    echo '<input type="hidden" name="sl_no[]" value="'.$sl_no.'" />'; 
} 
?>
<input name="update_rows" type="submit" value="Save">
</form>

<?php
if(isset($_POST['update_rows'])) {
    $nums = $_POST['sl_no'];
    $update = $_POST['update'];
    foreach($nums as $sl) {
        $sl_no = $sl;
        $val = $update[$sl_no];
        $sql_update = mysql_query("UPDATE guestdetails SET status = '$val' WHERE sl_no='$sl_no'");
    }
}
?>



回答3:


you are not submitting any id to status db.



来源:https://stackoverflow.com/questions/19195254/update-existing-row-in-mysql-using-php-coding

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!