使用php编码更新mysql中的现有行 [英] update existing row in mysql using php coding

查看:58
本文介绍了使用php编码更新mysql中的现有行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的数据库中,我有20多个列.我通过输入表单添加了19列,并成功存储在db中.我从主页上的db中获取了几行.在我的主页上还有1列.这是状态列,它是组合框类型.如果我单击状态列,它应该显示4个值.我想选择一个值,然后当我单击保存"按钮时,它必须转到具有相同ID的db中.怎么做?我试过了,但是在mysql db中没有更新...

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...

主页组合框编码:

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";

状态数据库编码:

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

帮我怎么做?

推荐答案

在您的IF中应该是$ _POST,而不是$ _GET另外,需要添加WHERE子句,如下所示:

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

此外,您使用过两次 update ,一次在< select> 中,一次在< input> 中,从< input> ,将其设为名称为 id slno 行的值的隐藏字段:

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";

这篇关于使用php编码更新mysql中的现有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆