I have retrieved data from my table and displayed them using the code below.
<?php require_once('../Connections/bidco.php'); ?>
<body>
<table width="671" height="43" border="1" align="center">
<table width="781" height="190" align="center">
<tr>
<td height="61" colspan="4"><div align="center"><strong> Inventory </strong></div></td>
</tr>
<tr>
<td width="77" height="68"><strong>ID</strong></td>
<td width="152"><strong>Item name.</strong> </td>
<td width="253"><strong>unit price</strong> </td>
<td width="253"><strong>Update price</strong></td>
</tr>
<?php
$query=mysql_query("SELECT *FROM manuf ") or die (mysql_error());
while($row=mysql_fetch_array($query))
{
$id=$row['id'];
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['itemname']; ?></td>
<td><?php echo $row['unitprice']; ?></td>
<td>change</td>
</tr>
<?php
}
?>
</table>
</body>
</html>
Now this PHP code is supposed to allow me to edit individual rows that have been displayed when i click on 'change' but it it not selecting the row. Any ideas how to solve this?
<?php require_once('../Connections/bidco.php'); ?>
<?php
$id = isset($_GET['id']) ? $_GET['id'] : null;
$query=mysql_query("SELECT * FROM manuf where id='$id' ")or die(mysql_error());
$row=mysql_fetch_array($query);
?>
<form action="updateprice.php" method="post" enctype="multipart/form-data">
<table align="center">
<tr>
<td> <label><strong>Item Name</strong></label></td>
<td> <input type='text' name='itemname' value=" <?php echo $row['itemname']; ?>" />
<input type="hidden" name="id" value="<?php echo $id; ?> " /> <br /></td>
</tr>
<tr>
<td><label><strong>Unit price </strong></label></td>
<td> <input type="text" name="unitprice" value="<?php echo $row['unitprice']; ?> " /><br /></td>
</tr>
<tr>
<td>
<input type="reset" name="Reset" value="CANCEL" />
<br></td>
<td>
<input type="submit" name="Submit2" value="Update" /> </td>
</tr>
</table>
</form>
</body>
</html>
Thank you in advance
Your forget to add you id to your link
<td>change</td>
I think you need
<a href="change.php?id=$row['id']">
Edit - use Arif_suhail_123's answer - I did not notice you have PHP mixed in with HTML
You need to pass rowid to href of Edit Link.
<td>change</td>
You are looking for id in change.php, but you are not sending that in the header. You must change change with
change
Also, I suggest you STOP using mysql_* commands, since the are Depreceted, and will no longer be supported. Use mysqli_* commands instead.
Related
I am trying to fetch records from my table, but it shows all the records at once.
I want a single row data to be viewed. After clicking on next button, it jumps to the next row. How can we achieve this?
<?php include("conect.php"); ?>
<?php
session_start();
if($_SESSION['u_name'==''])
{
echo "log in failed";
echo '<script> window.locaion="index.php";</script>';
}?>
<?php
$query=mysql_query("select * from question_paper ");
$i=1;
while($res= mysql_fetch_array($query))
{
$id=$row['main_id'];
$question=$res['question'];
$optionA=$res['option1'];
$optionB=$res['option2'];
$optionC=$res['option3'];
$optionD=$res['option4'];
?>
<html>
<head><title>exam</title></head>
<body>
<form name="student_form" action="" method="post">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="36" colspan="2" bgcolor="#85A157"> (<?php echo $i;?>)<?php echo $question;?> </td>
</tr>
<tr>
<td colspan="2"><table width="100%" height="64" border="2" cellpadding="0" cellspacing="0" bordercolor="#A6BF79">
<tr>
<td height="32" width="50%"><input type="radio" name="RadioGroup<?php echo $i;?>" value="<?php echo $optionA;?>" id="optionA" />
<?php echo $optionA;?></td>
<td width="50%"><input type="radio" name="RadioGroup<?php echo $i;?>" value="<?php echo $optionC;?>" id="optionC" />
<?php echo $optionC;?></td>
</tr>
<tr>
<td height="32" width="50%"><input type="radio" name="RadioGroup<?php echo $i;?>" value="<?php echo $optionB;?>" id="optionB" />
<?php echo $optionB;?></td>
<td width="50%"><input type="radio" name="RadioGroup<?php echo $i;?>" value="<?php echo $optionD;?>" id="optionD" />
<?php echo $optionD;?></td>
</tr>
</table></td>
</tr>
</table>
<?php
$i=$i+1;
}
?>
<?php
$records = mysql_num_rows($query);
if($records!=0)
{
?>
<input type="submit" name="button" id="button" value="Submit" />
<?php
}
?>
</form>
</body>
</html>
Its better to use pagination and with select query use limit and for pagination
see the following link
http://www.tutorialspoint.com/php/mysql_paging_php.htm
$sql="SELECT * FROM tbl_name WHERE id >= (SELECT FLOOR( MAX(id) * RAND()) FROM tbl_name ) ORDER BY id LIMIT $no_of_question_you_want to select";
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I have been playing for quite a few hours, but to no avail, I have been building a script that will allow me to edit info on a webpage in the admin by editing it in a table. I used some tutorials etc, but now it just doesnt want to update the database.
I have three elements:
FIRST ONE: Table of listings - works fine
require_once("models/config.php");
if (!securePage($_SERVER['PHP_SELF'])){die();}
require_once("models/header.php");
// Connects to your Database
mysql_connect("localhost", "cl52-abcdef","abcdef") or die(mysql_error());
mysql_select_db("cl52-abcdef") or die(mysql_error());
$tbl_name="DealOne";
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
<table width="400" border="1" cellspacing="0" cellpadding="3">
<tr>
<td colspan="4"><strong>List data from mysql </strong> </td>
</tr>
<tr>
<td align="center"><strong>Desc</strong></td>
<td align="center"><strong>Dest</strong></td>
<td align="center"><strong>RRP</strong></td>
<td align="center"><strong>Price Entry</strong></td>
<td align="center"><strong>Entries Avail</strong></td>
<td align="center"><strong>Update</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td><? echo $rows['holdesc1']; ?></td>
<td><? echo $rows['holdest1']; ?></td>
<td><? echo $rows['rrp1']; ?></td>
<td><? echo $rows['cpe1']; ?></td>
<td><? echo $rows['ea1']; ?></td>
<td align="center">update</td>
</tr>
<?php
}
?>
</table>
</td>
</tr>
</table>
<?php
mysql_close();
?>
Second One: where the form is to update the data
require_once("models/config.php");
if (!securePage($_SERVER['PHP_SELF'])){die();}
require_once("models/header.php");
// Connects to your Database
mysql_connect("localhost", "cl52-abcdef","abcdef") or die(mysql_error());
mysql_select_db("cl52-abcdef") or die(mysql_error());
$tbl_name="DealOne";
// get value of id that sent from address bar
$id=$_GET['id'];
// Retrieve data from database
$sql="SELECT * FROM $tbl_name 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_ac.php">
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td> </td>
<td colspan="3"><strong>Update data in mysql</strong> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"><strong>Desc</strong></td>
<td align="center"><strong>Dest</strong></td>
<td align="center"><strong>RRP</strong></td>
<td align="center"><strong>Price Entry</strong></td>
<td align="center"><strong>Entries Avail</strong></td>
</tr>
<tr>
<td> </td>
<td align="center"><input name="desc" type="text" id="holdesc1" value="<? echo $rows['holdesc1']; ?>" size="35">
</td>
<td align="center">
<input name="Destination" type="text" id="holdest1" value="<? echo $rows['holdest1']; ?>" size="35">
</td>
<td>
<input name="RRP" type="text" id="rrp1" value="<? echo $rows['rrp1']; ?>" size="8">
</td>
<td align="center">
<input name="Price per Entry" type="text" id="cpe1" value="<? echo $rows['cpe1']; ?>">
</td>
<td align="center">
<input name="Entries Available" type="text" id="ea1" value="<? echo $rows['ea1']; ?>" size="8">
</td>
</tr>
<tr>
<td> </td>
<td>
<input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>">
</td>
<td align="center">
<input type="submit" name="Submit" value="Submit">
</td>
<td> </td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<?php
// close connection
mysql_close();
?>
And finally the third one, that I believed would update the database, but it doesnt:
require_once("models/config.php");
if (!securePage($_SERVER['PHP_SELF'])){die();}
require_once("models/header.php");
// Connects to your Database
mysql_connect("localhost", "cl52-abcdef","abcdef") or die(mysql_error());
mysql_select_db("cl52-abcdef") or die(mysql_error());
$tbl_name="DealOne";
// update data in mysql database
$sql="UPDATE $tbl_name SET holdesc1='$holdesc1', holdest1='$holdest1', rrp1='$rrp1', cpe1='$cpe1', ea1='$ea1' WHERE id='$id'";
$result=mysql_query($sql);
// if successfully updated.
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='view_posts.php'>View result</a>";
}
else {
echo "ERROR";
}
?>
Any ideas or suggestions on how to get this to work? Thanks in advance!!
Your $tbl_name isn't quoted.
You have
$sql="UPDATE $tbl_name SET holdesc1='$holdesc1', holdest1='$holdest1', rrp1='$rrp1', cpe1='$cpe1', ea1='$ea1' WHERE id='$id'";
I believe it should be:
$sql="UPDATE '$tbl_name' SET holdesc1='$holdesc1', holdest1='$holdest1', rrp1='$rrp1', cpe1='$cpe1', ea1='$ea1' WHERE id='$id'";
The issue here is that you're rendering a single form to update multiple entries, all of which share attributes with the same id HTML attribute. Your update script can't distinguish between DOM elements with the same id, therefore, your records won't be updated correctly – even if you're identifying each entry by passing the id as a hidden attribute.
A possible solution would be to pass the entry's unique ID as part of each of that entry's attributes' id:
<input name="Price per Entry" type="text" id="cpe1_<? echo $rows['id']; ?>" value="<? echo $rows['cpe1']; ?>">
This creates distinct id elements for each form field. Then, in you update script, you can append the entry's id to the element id attribute so that it correctly associates the entry's attribute with the correct form field.
i want to insert this below given form data to database, but i dont know how to do this, it is about inserting product to multiple warehouse with different quantities, when checkbox against the warehouse name is checked than the quantity textbox appears.
and this is my PHP code
<form name="form1" method="post" action="product_insert.php" enctype="multipart/form-data">
<table>
<tr>
<td width="274" align="right" height="25"><strong>Product Name :</strong></td>
<td><input type="text" name="wproname" value="" /></td>
</tr>
<tr>
<td width="274" align="right" height="25"><strong>Select Warehouse :</strong></td>
<td width="500"><table style="border:none">
<tr >
<td> Select </td>
<td> Name </td>
<td> Quantity </td>
</tr>
<?php
$sql="select * from tbl_warehouse where w_flag='1'";
$result=ExecuteGetRows($sql);
$num_rows=count($result); ?>
<?php for($i=0;$i<$num_rows;$i++){ ?>
<tr>
<td><input type="checkbox" name="chk<?php echo $result[$i]['w_id'];?>" value="<?php echo $result[$i]['w_id'];?>" id="chk<?php echo $result[$i]['w_id'];?>" onChange="display<?php echo $result[$i]['w_id'];?>();" />
</td>
<td><?php echo $result[$i]['w_name'];?></td>
<td><input type="text" name="qty<?php echo $result[$i]['w_id'];?>" id="qty<?php echo $result[$i]['w_id'];?>" style="display:none" />
</td>
</tr>
<script>
function display<?php echo $result[$i]['w_id'];?>()
{
if(document.getElementById("chk<?php echo $result[$i]['w_id'];?>").checked)
{
document.getElementById("qty<?php echo $result[$i]['w_id'];?>").style.display="block";
}
if(!document.getElementById("chk<?php echo $result[$i]['w_id'];?>").checked)
{
document.getElementById("qty<?php echo $result[$i]['w_id'];?>").style.display="none";
}
}
</script>
<?php } ?>
</table></td>
</tr>
<tr>
<td align="right"></td>
<td width="296"><input type="submit" name="Submit" value="Add New" align="middle" class="button login_btn"/></td>
</tr>
</table>
</form>
And I am using this php code for inserting data to the database
$sqls=mysql_query("select * from tbl_warehouse");
while($result=mysql_fetch_array($sqls)) {
$w = $result['w_id'];
echo $_POST['chk'.$w];
foreach($_POST['chk'.$w] as $key=>$val) {
echo $_POST['chk'.$w];
$sql = "INSERT INTO tbl_product(p_id,w_id,p_name,p_qty) values ('','".$wid."','".$wproname."','".$qty."')";
mysql_query($sql);
}
}
You don't need the leading comma in your values list in your insert statement. So change this:
"INSERT INTO tbl_product(p_id,w_id,p_name,p_qty) values ('','".$wid."','".$wproname."','".$qty."')";
To this:
"INSERT INTO tbl_product(p_id,w_id,p_name,p_qty) values ('".$wid."','".$wproname."','".$qty."')";
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 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();
?>