I want to change data in a sql table by using checkbox. I have successfully echo all the data of my table. but, I can not delete. after I enter the new data et click on checkbox and submit. the data go back to the orignial data. can anyone help me please? thank you so much.
<?php
include_once("mesparametres.inc.php");
$sql="SELECT * FROM poisson";
$result=mysql_query($sql);
// Count table rows
$count=mysql_num_rows($result);
?>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<form name="form1" method="post" action="modifier.php">
<tr>
<td>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<tr>
<td align="center"><strong>Id</strong></td>
<td align="center"><strong>Nom</strong></td>
<td align="center"><strong>Classe</strong></td>
<td align="center"><strong>eau</strong></td>
<td align="center"><strong>nourriture</strong></td>
<td align="center"><strong>couleur</strong></td>
<td align="center"><strong>taille</strong></td>
<td align="center"><strong>vie</strong></td>
<td align="center"><strong>dateacqui</strong></td>
<td align="center"><strong>modifier</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result))
{
?>
<tr>
<td align="center"><input type="hidden" name="id<? echo $rows['id']; ?>" value="<? echo $rows['id']; ?>" /><? echo $rows['id']; ?></td>
<td align="center"><input name="nom<? echo $rows['id']; ?>" type="text" id="nom" value="<? echo $rows['nom']; ?>"></td>
<td align="center"><input name="classe<? echo $rows['id']; ?>" type="text" id="classe" value="<? echo $rows['classe']; ?>"></td>
<td align="center"><input name="eau<? echo $rows['id']; ?>" type="text" id="eau" value="<? echo $rows['eau']; ?>"></td>
<td align="center"><input name="nourriture<? echo $rows['id']; ?>" type="text" id="nourriture" value="<? echo $rows['nourriture']; ?>"></td>
<td align="center"><input name="couleur<? echo $rows['id']; ?>" type="text" id="couleur" value="<? echo $rows['couleur']; ?>"></td>
<td align="center"><input name="taille<? echo $rows['id']; ?>" type="text" id="taille" value="<? echo $rows['taille']; ?>"></td>
<td align="center"><input name="vie<? echo $rows['id']; ?>" type="text" id="vie" value="<? echo $rows['vie']; ?>"></td>
<td align="center"><input name="dateacqui<? echo $rows['id']; ?>" type="text" id="dateacqui" value="<? echo $rows['dateacqui']; ?>"></td>
<td align="center"><input name="modifier[]>" type="checkbox" id="modifier[]" value="<? echo $rows['id']; ?>" ></td>
</tr>
<?php if(isset($_POST['checkbox'])){$checkbox = $_POST['checkbox'];}?>
<?php
}
?>
<tr>
<td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
// Check if button name "Submit" is active, do this
if($Submit)
{
foreach($_POST['id'] as $id)
{
$sql1="UPDATE poisson SET nom='".$_POST["nom".$id]."', classe='".$_POST["classe".$id]."', eau='".$_POST["eau".$id]."',nourriture='".$_POST["nourriture".$id]."',couleur='".$_POST["couleur".$id]."',taille='".$_POST["taille".$id]."',vie='".$_POST["vie".$id]."',dateacqui='".$_POST["dateacqui".$id]."' WHERE id='".$id."'";
$result1=mysql_query($sql1);
}
}
mysql_close();
?>
</body>
</html>
Checkboxes are checked or not based on the checked HTML attribute, not the value attribute:
This:
<input type="checkbox" checked="checked" />
Not this:
<input type="checkbox" value="1" />
The value is what gets submitted along with the form when, for example, you group a bunch of checkboxes by using the same name attribute.
First I'd suggest to store that POST-data in variables for clarity :)
Then try run the query without storing it, i.e:
mysql_query("UPDATE table SET nom='variable1', classe='variable2' WHERE id='condition';
and so on, just like you did. You need also the attribute 'checked' i.e. checked="yes"
Related
I have this code which permits me to display all the data in the database as a textarea, I need to update them by clicking a update button!
Based on this one, is supposed to make me edit them, but when i click submit it doesn't...
<form name="form1" method="post" action="">
<tr>
<td>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<tr>
<td align="center"><strong>Id</strong></td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Lastname</strong></td>
<td align="center"><strong>Email</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center">
<? $id[]=$rows['id']; ?>
<? echo $rows['id']; ?>
</td>
<td align="center">
<input name="name[]" type="text" id="name" value="<? echo $rows['name']; ?>">
</td>
<td align="center">
<input name="lastname[]" type="text" id="lastname" value="<? echo $rows['lastname']; ?>">
</td>
<td align="center">
<input name="email[]" type="text" id="email" value="<? echo $rows['email']; ?>">
</td>
</tr>
<?php
}
?>
<tr>
<td colspan="4" align="center">
<input type="submit" name="Submit" value="Submit"/>
</td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
// Check if button name "Submit" is active, do this
if($Submit)
{
for($i=0;$i<$count;$i++)
{
$sql1="UPDATE $tbl_name SET name='$name[$i]', lastname='$lastname[$i]', email='$email[$i]' WHERE id='$id[$i]'";
$result1=mysql_query($sql1);
}
}
if($result1)
{
header("location:update_multiple.php");
}
Yes because you forget your id's
<? $id[]=$rows['id']; ?> cannot be passed like that
<input type="hidden" name="id[]" value ="<?php echo $rows['id']; ?>" /><? echo $rows['id']; ?>
and script if($Submit){ should be if($_POST['Submit'] != ''){
You aren't defining $Submit in your post, so the stuff in the { ... } is never executed.
You should try something like this for your update:
if(isset($_POST[$name]))
{
// update stuff
}
In your code, as the if statement never executes, $result is never set, so the user isn't redirected away - it will just show the same page each time.
To check if a post occur when clicking a button should be set as follow:
In the <form>tag add the following
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
And lastly, where you check if the button was clicked:
if(isset($_POST['Submit'])) {
//Update fields
}
Remember that the submit button name field is case sensitive in php
I am trying to update a data in a table which has 5 fields.
The fields are the following: proj_name, cust_name, address, cost and details.
Here is my code for the update (update_orde.php):
<?php
include("connect.php");
// get value of id that sent from address bar
$id=$_GET['id'];
// Retrieve data from database
$sql="SELECT * FROM project WHERE id='$id'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<form name="form1" method="post" action="update_orde_suc.php">
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td colspan="5"><strong>Update Data</strong> </td>
</tr>
<tr>
<td align="center"><strong>Project Name</strong></td>
<td align="center"><strong>Customer</strong></td>
<td align="center"><strong>Address</strong></td>
<td align="center"><strong>Cost</strong></td>
<td align="center"><strong>Details</strong></td>
</tr>
<tr>
<td><input name="pn" type="text" id="pn" value="<? echo $rows['proj_name']; ?>"></td>
<td align="center"><input name="cn" type="text" id="cn" value="<? echo $rows['cust_name']; ?>" size="15"></td>
<td align="center"><input name="add" type="text" id="add" value="<? echo $rows['address']; ?>" size="15"></td>
<td><input name="cost" type="text" id="cost" value="<? echo $rows['cost']; ?>" size="15"></td>
<td><input name="details" type="text" id="details" value="<? echo $rows['details']; ?>" size="15"></td>
</tr>
<tr>
<td align="center" colspan="5"><input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>"> <input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<?
// close connection
mysql_close();
?>
and here's the successful insertion into the database (update_orde_suc.php):
<?php
include("connect.php");
$id=$_POST['id'];
$pn = $_POST['proj_name'];
$cn = $_POST['cust_name'];
$add = $_POST['address'];
$cost = $_POST['cost'];
$det = $_POST['details'];
// update data in mysql database
$sql="UPDATE project SET proj_name='$pn', cust_name='$cn',address='$add', cost='$cost', details='$det' WHERE id='$id'";
$result=mysql_query($sql);
// if successfully updated.
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='orders_edit.php'>View result</a>";
}
else {
echo "ERROR";
}
?>
The problem is that when I try to change the data there is an error that says the 3 first indexes of update_orde_suc.php are undefined (cost and details are ok).
The weirdest thing is that I used the exact same code for another table update and it worked just fine, the only thing i did now was to change the names of the variables to correspond to the names of the new table.
You mixed up the variables badly. For example you save the proj_name coming from input as $pn and later when you are inserting it in the database you use $proj_name. You have to stick with the variable names throughout the code to work.
I created a html form to enable users to update data. However the input data (array values) are not passed through to php SUBMIT, thus clicking SUBMIT does not update the table. When I go into the SUBMIT portion of the script and change the SET to specific numbers or text, the table is updated. Meaning that the values from the html input data array are not being passed through properly to the SUBMIT portion of the script. Any help appreciated.
<?php
//Mysql connection and initial select query placed above
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<form name="Contacts" method="post" action="">
<tr>
<td>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td align="center"><strong>Id</strong></td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Lastname</strong></td>
<td align="center"><strong>Email</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center"><?php $id[]=$rows['id']; ?><?php echo $rows['id']; ?></td>
<td align="center"><input name="name[]" type="text" id="name" value="<?php echo $rows['name']; ?>"></td>
<td align="center"><input name="lastname[]" type="text" id="lastname" value="<?php echo $rows['lastname']; ?>"></td>
<td align="center"><input name="email[]" type="text" id="email" value="<?php echo $rows['email']; ?>"></td>
</tr>
<?php
}
?>
<tr>
<td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
// Check if button name "Submit" is active, do this
if(isset($_POST['Submit'])){
$id=$_POST['id'];
$name=$_POST['name'];
for($i=0;$i<$num;$i++){
$sql1="UPDATE contacts SET name= ".$name[$i]." WHERE id= ".$id[$i]."";
$result1=mysql_query($sql1);
}
}
if($result1){
header("location:updated.php");
}
mysql_close();
?>
Thanks!
You are missing single quotes around your $name[$i] in the SQL statement. If id is not always numeric, you will also need to surround $id[$i] in single quotes.
$sql1="UPDATE contacts SET name= '".$name[$i]."' WHERE id= ".$id[$i]."";
//------------------------------^^^-----------^^^
Some error checking in your mysql_query() call would make this clearer. See below.
And you must filter these against SQL injection before passing them to the query.
for($i=0;$i<$num;$i++) {
// Call mysql_real_escape_string() to sanitize these...
$id[$i] = mysql_real_escape_string($id[$i]);
$name[$i] = mysql_real_escape_string($name[$i]);
$sql1="UPDATE contacts SET name= '".$name[$i]."' WHERE id= ".$id[$i]."";
$result1 = mysql_query($sql1);
// Error checking:
if (!$result1) {
echo mysql_error();
}
}
My Mistake. I didn't look at the form enough. You are assigning an array here. PHP is easy to debug-
Right here:
$id=$_POST['id'];
$name=$_POST['name'];
After those lines use var_dump($id) or print_r($id) to check out the contents in your variables.
Thanks very much for the prompt responses and the assistance provided. After implementing the recommended changes, the final working script (using PHP 5.2) is as shown below (for anyone who might need it).
<?php
Mysql connection, initial query and then close Mysql connection
?>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<form name="Contacts" method="post" action="">
<tr>
<td>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<tr>
<td align="center"><strong>Id</strong></td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Lastname</strong></td>
<td align="center"><strong>Email</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center"><?php $id[]=$rows['id']; ?><?php echo $rows['id']; ?></td>
<td align="center"><input name="name[]" type="text" id="name" value="<?php echo $rows['name']; ?>"></td>
<td align="center"><input name="lastname[]" type="text" id="lastname" value="<?php echo $rows['lastname']; ?>"></td>
<td align="center"><input name="email[]" type="text" id="email" value="<?php echo $rows['email']; ?>"></td>
</tr>
<?php
}
?>
<tr>
<td height="75" colspan="4" align="center"><input type="submit" name="Submit" value=" save for later "></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
// Check if button name "Submit" is active, do this
if(isset($_POST['Submit'])){
mysql_connect($dbhost,$username,$password);
#mysql_select_db($database) or die( "Unable to select database");
$name=$_POST['name'];
for($i=0;$i<$num;$i++) {
// sanitize...
$id[$i] = mysql_real_escape_string($id[$i]);
$name[$i] = mysql_real_escape_string($name[$i]);
$sql1="UPDATE test_mysql SET name= '".$name[$i]."' WHERE id= ".$id[$i]."";
$result1 = mysql_query($sql1);
// Error checking:
if (!$result1) {
echo mysql_error();
}
}
if($result1){
header("location:updated.php");
}
}
mysql_close();
?>
I am doing a online test I fetch question and option from database....
I need to select which option they choosed and question id....to the next page for the answered question or not.....I got only they selected option I need get all value....
my php code as follows
<tr>
<td height="30"><?= $id?></td>
<td height="30" colspan="2"><?= $question ?></td>
</tr>
<?php
if($option1!='') { ?>
<tr>
<td height="30"><input type="radio" name="answer[<? echo $id?>]" value="<?php echo $id?>-<?php echo $option1?>" /></td>
<td height="30" colspan="2"><?= $option1?></td>
</tr>
<?php }?>
<?php if($option2!='') {?>
<tr>
<td height="30"><input type="radio" name="answer[<? echo $id?>]" value="<?php echo $id?>-<?php echo $option2?>" /></td>
<td height="30" colspan="2"><?= $option2?></td>
</tr><?php }?>
<?php if($option3!='') {?>
<tr>
<td height="30"><input type="radio" name="answer[<? echo $id?>]" value="<?php echo $id?>-<?php echo $option3?>" /></td>
<td height="30" colspan="2"><?= $option3?></td>
</tr><?php }?>
<?php if($option4!='') {?>
<tr>
<td height="30"><input type="radio" name="answer[<? echo $id?>]" value="<?php echo $id?>-<?php echo $option4?>" /></td>
<td height="30" colspan="2" ><?= $option4?></td>
</tr>
<? }
$id will have the same value for all your options.
So you might want to put name="answer" instead:
<input type="radio" name="answer" value="<?php echo $id?>-<?php echo $option2?>" />
On your result page $_GET['answer'] should then have the correct value.
You could send all the answers as hidden elements:
<input type="hidden" name="allanswers[]" value="Answer 1" />
<input type="hidden" name="allanswers[]" value="Answer 2" />
<input type="hidden" name="allanswers[]" value="Answer 3" />
<input type="hidden" name="allanswers[]" value="Answer 4" />
i try to update a mysql table with multiple check box, but my code doesn't seem to work, so any help is welcome. My code is:
<strong>Update <strong class="highlight">multiple</strong> <strong class="highlight">rows</strong> <strong class="highlight">in</strong> <strong class="highlight">mysql</strong></strong><br>
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="root"; // Mysql password
$db_name="db_test"; // Database name
$tbl_name="test_table"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
// Count table <strong class="highlight">rows</strong>
$count=mysql_num_rows($result);
?>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<form name="form1" method="post" action="<? echo $_SERVER['REQUEST_URI']; ?>">
<tr>
<td>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<tr>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Description</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Phone Number</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Operation</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result))
{
?>
<tr>
<td align="center"><input type="hidden" name="id[]" value="<? echo $rows['id']; ?>" /><? echo $rows['id']; ?></td>
<td align="center"><input name="name<? echo $rows['id']; ?>" type="text" id="name" value="<? echo $rows['name']; ?>"></td>
<td align="center"><input name="email<? echo $rows['id']; ?>" type="text" id="email" value="<? echo $rows['email']; ?>"></td>
<td align="center"><input name="description<? echo $rows['id']; ?>" type="text" id="description" value="<? echo $rows['description']; ?>"></td>
<td align="center"><input name="phone_number<? echo $rows['id']; ?>" type="text" id="phone_number" value="<? echo $rows['phone_number']; ?>"></td>
<td align="center"><input name="operation<? echo $rows['id']; ?>" type="text" id="operation" value="<? echo $rows['operation']; ?>"></td>
<td align="center"><input name="ONOFF<? echo $rows['id']; ?>" type="checkbox" id="ONOFF" value="1"
<?php if ($rows['ONOFF'] ==1) { echo "checked";} else {} ?>
</td>
</tr>
<?php
}
?>
<tr>
<td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
// Check if button name "Submit" is active, do this
if($Submit)
{
foreach($_POST['id'] as $id)
{
$onoff = 0;
if (isset($_POST["ONOFF".$id]))
{
$onoff = 1;
}
$sql1="UPDATE ".$tbl_name." SET name='".$_POST["name".$id]."', email='".$_POST["email".$id]."', description='".$_POST["description".$id]."', phone_number='".$_POST["phone_number".$id]."', operation='".$_POST["operation".$id]."', ONOFF='".$onoff."' WHERE id='".$id."'";
$result1=mysql_query($sql1);
}
}
if($result1){
header("location:test_update2.php");
}
mysql_close();
?>
Thanks for you help.
Regards.
Useif($_POST['Submit']) instead of if($Submit)
Second thing is that you should use id="name" only once, and you have id="name" on every row.
As far as I understood, after the user form submission you want to redirect the user to test_update2.php and the problem was that you couldn't simply do that since the headers were already been send. You can't ever use header() method after HTML, because you can't ever get control of the headers after you output some HTML.
I fixed your code and tested it out and it was working perfectly.
EDITED:
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="root"; // Mysql password
$db_name="db_test"; // Database name
$tbl_name="test_table"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Check if button name "Submit" is active, do this
if(isset($_POST['Submit'])) {
foreach($_POST['id'] as $id) {
$onoff = 0;
if (isset($_POST["ONOFF".$id])) {
$onoff = 1;
}
if($onoff == 1) {
$sql1="UPDATE ".$tbl_name." SET name='".$_POST["name".$id]."', email='".$_POST["email".$id]."', description='".$_POST["description".$id]."', phone_number='".$_POST["phone_number".$id]."', operation='".$_POST["operation".$id]."', ONOFF='".$onoff."' WHERE id='".$id."'";
} else {
$sql1="UPDATE ".$tbl_name." SET ONOFF='".$onoff."' WHERE id='".$id."'";
}
$result1=mysql_query($sql1);
}
}
//get data from DB
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
// Count table <strong class="highlight">rows</strong>
$count=mysql_num_rows($result);
?>
<strong>Update <strong class="highlight">multiple</strong> <strong class="highlight">rows</strong> <strong class="highlight">in</strong> <strong class="highlight">mysql</strong></strong><br>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<form name="form1" method="post" action="<? echo $_SERVER['REQUEST_URI']; ?>">
<tr>
<td>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<tr>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Description</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Phone Number</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Operation</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result))
{
?>
<tr>
<td align="center"><input type="hidden" name="id[]" value="<? echo $rows['id']; ?>" /><? echo $rows['id']; ?></td>
<td align="center"><input name="name<? echo $rows['id']; ?>" type="text" id="name" value="<? echo $rows['name']; ?>"></td>
<td align="center"><input name="email<? echo $rows['id']; ?>" type="text" id="email" value="<? echo $rows['email']; ?>"></td>
<td align="center"><input name="description<? echo $rows['id']; ?>" type="text" id="description" value="<? echo $rows['description']; ?>"></td>
<td align="center"><input name="phone_number<? echo $rows['id']; ?>" type="text" id="phone_number" value="<? echo $rows['phone_number']; ?>"></td>
<td align="center"><input name="operation<? echo $rows['id']; ?>" type="text" id="operation" value="<? echo $rows['operation']; ?>"></td>
<td align="center"><input name="ONOFF<? echo $rows['id']; ?>" type="checkbox" id="ONOFF" value="1"
<?php if ($rows['ONOFF'] ==1) { echo "checked";} else {} ?>
</td>
</tr>
<?php
}
?>
<tr>
<td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
//close mysql connection
mysql_close();
?>