简体   繁体   中英

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. 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. You should switch to mysqli or 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

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

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