[英]Update a MySQL Database with a Form
I'm trying to create a form that allows a user to select a field from a drop down box and then change what is currently written in the field. 我正在尝试创建一种表单,允许用户从下拉框中选择一个字段,然后更改当前写在该字段中的内容。
My current code allows me to view the drop down list select the field I want to change and then enter my new text into a box. 我当前的代码允许我查看下拉列表,选择要更改的字段,然后在框中输入新文本。 But when I click update, nothing happens. 但是,当我单击更新时,什么也没有发生。
<?php
mysql_connect("", "", "") or die(mysql_error());
mysql_select_db("") or die(mysql_error());
$query = "SELECT * FROM news_updates";
$result=mysql_query($query) or die("Query Failed : ".mysql_error());
$i=0;
while($rows=mysql_fetch_array($result))
{
$roll[$i]=$rows['Text'];
$i++;
}
$total_elmt=count($roll);
?>
---------------------------------------------------------Now I have the form
<form method="POST" action="">
Select the news post to Update: <select name="sel">
<option>Select</option>
<?php
for($j=0;$j<$total_elmt;$j++)
{
?><option><?php
echo $roll[$j];
?></option><?php
}
?>
</select><br />
Text Field: <input name="username" type="text" /><br />
<input name="submit" type="submit" value="Update"/><br />
<input name="reset" type="reset" value="Reset"/>
</form>
-----------------------------------------------Now I have the update php
<?php
if(isset($_POST['submit']))
{
$username=$_POST['username'];
$query2 = "UPDATE news_updates SET username='$username' WHERE rollno='$value'";
$result2=mysql_query($query2) or die("Query Failed : ".mysql_error());
echo "Successfully Updated";
}
?>
Well, you seem to be missing the $value
part. 好吧,您似乎缺少$value
部分。 Something like this should do, for the last part: 对于最后一部分,应该执行以下操作:
<?php
if(isset($_POST['submit']))
{
$username = mysql_real_escape_string($_POST['username']);
$value = mysql_real_escape_string($_POST['sel']);
$query2 = "UPDATE news_updates SET username='$username' WHERE rollno='$value'";
echo $query2; //For test, to see what is generated, and sent to database
$result2=mysql_query($query2) or die("Query Failed : ".mysql_error());
echo "Successfully Updated";
}
?>
Also, you should not use mysql_* functions as they are deprecated. 另外,不应该使用不推荐使用的mysql_ *函数。 You should switch to mysqli or PDO. 您应该切换到mysqli或PDO。
You need to change this 你需要改变这个
<?php
for($j=0;$j<$total_elmt;$j++)
{
?><option><?php
echo $roll[$j];
?></option><?php
}
to this 对此
<?php
for($j=0;$j<$total_elmt;$j++)
{
?><option value="<?php echo $roll[$j];?>"> <?php echo $roll[$j];?></option> <?php
}
And you also need to change the update query from this 而且您还需要从中更改更新查询
$query2 = "UPDATE news_updates SET username='$username' WHERE rollno='$value'";
to this 对此
$query2 = "UPDATE news_updates SET username='$username' WHERE rollno='".$_POST['sel']."'";
NB: Here I am assuming that $_POST['sel']
has the value selected by the user from the drop down menu because I could not find anything which corresponds to $value
注意:这里我假设$_POST['sel']
具有用户从下拉菜单中选择的值,因为我找不到与$value
相对应的任何内容
First, try adding a value to your options, like so: 首先,尝试为您的选项添加一个值,如下所示:
for($j=0;$j<$total_elmt;$j++)
{
?>
<option value="<?php echo $roll['id']; ?>"><?php echo $roll['option_name']; ?></option>
<?php
}
Then, when you parse your file, go like so: 然后,当您解析文件时,如下所示:
$value = $_POST['sel']; // add any desired security here
That should do it for you 那应该为你做
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.