Hi I am coming into an issue in my form when i try to set the value of my field
to a value of a variable it works exactly how i want it for my product field but nothing is showing up in the price field in my form
any help is appreciated i have benn at it for a good while
$query = "SELECT * FROM suplements WHERE product = '$product' OR name = '$prodname' OR price = '$price' OR image = '$image'";
$result = #mysql_query($query) or die ("could not execute SQL query");
while($row = mysql_fetch_array($result))
{ ?>
<table>
<tr>
<th>Product :<?php echo $row["product"]?></th>
</tr>
<td><img src = "<?php echo $row["image"] ?>"height="400" width="400"><?php ?>
<td>
<form>
<input type="text" name="qty" value="1" size="2"><br>
Product <input type="text" name="product" value="<?php echo htmlentities($product); ?>" /></br>
Price <input type="text" name="price" value="<?php echo htmlentities($price); ?>" />
<input type="submit" name="submit" value="ADD" >
</form>
<?php echo $row["information"];?></td>
<tr><td><?php echo $row["product"];?>
<?php echo $row["name"];?>
<?php echo $row["drand"];?>
<?php echo $row["weight"];?>
<td><?php echo $row["price"];?></td></tr>
</tr>
</table>
Is this what you are looking for?
Product <input type="text" name="product" value="<?php echo htmlentities($row['product']); ?>" /></br>
Price <input type="text" name="price" value="<?php echo htmlentities($row['price']); ?>" />
For debug sake, do this after the while: "print_r($row);" - this will show you the exact contents of the $row variable
Plus... it would be nice to add limit 1 to your query
Related
I am in the middle of creating a CMS in PHP & MySQL for the company I work for and have hit a bump in the road.
So I have a table that fetches and lists everything from a table, then next to each record is an edit button.
This button has the following code:
<?php
echo '<td><form method="POST" action="editRecord.php?id='.$id.'">
<input type="submit" name="Edit"></form></td>';
?>
Which then obviously goes to that page, however on the next page i have the following code and it's not running anything.
I don't understand why it's not fetching the information from the database if I have given it the ID...
include 'login/connection.php';
$id = $row['id'];
$result = $db->query("SELECT * FROM vehicleOrderForm WHERE id = ' $id ' ");
$row = mysqli_fetch_array($result);
That code is at the start and then further down is this;
<?php
echo $row["id"];
$id = $row["id"];
$make = $row["make"];
$varient = $row["varient"];
$stockno = $row["stockno"];
$transmission = $row["transmission"];
$cc = $row["cc"];
$colour = $row["colour"];
$delivery = $row["delivery"];
$stock = $row["stock"];
$sold = $row["sold"];
$customer = $row["customer"];
$tax = $row["tax"];
$comments = $row["comments"];
?>
<div>
<h1><?php echo $id; ?></h1>
</div>
<form method="POST" action="">
<p>Make:</p>
<?php echo '<input type="text" name="make" value="'.$make.'" />;' ?>
<p>Varient:</p>
<input type="text" name="varient" value=" <?php echo $varient; ?>" /> <br />
<p>Stock Number:</p>
<input type="email" name="stockno" value="<?php echo $stockno; ?>" /> <br />
<p>Transmission:</p>
<input type="number" name="transmission" value="<?php echo $transmission; ?>" /> <br />';
<p>CC:</p>
<input type="username" name="cc" value="<?php echo $cc; ?>" /> <br />';
<p>Colour:</p>
<input type="text" name="colour" value="<?php echo $colour; ?>" /> <br />';
<p>Expected Delivery:</p>
<input type="text" name="delivery" value="<?php echo $delivery; ?>"/> <br />';
<p>In Stock:</p>
<input type="text" name="stock" value="<?php echo $stock; ?>"/> <br />';
<p>Status:</p>
<input type="text" name="sold" value="<?php echo $sold; ?>"/> <br />';
<p>Customer:</p>
<input type="text" name="customer" value="<?php echo $customer; ?>"/> <br />';
<p>Tax:</p>
<input type="text" name="tax" value="<?php echo $tax; ?>"/> <br />';
<p>Comments:</p>
<input type="text" name="comments" value="<?php echo $comments; ?>"/> <br />';
<input type="Submit" name="Edit" value="Edit">';
</form>
If anyone can understand it, that would be great.
That form around the Edit button looks like it's totally useless.. You can't sent the form with POST method, and attach an id to the action url: that param won't be sent.
Solution 1: If you can, I would suggest you to switch to GET param and use an anchor. Like this:
<?php echo '<td>Edit</td>'; ?>
And edit your code on page editRecord.php as follows:
$id = $_GET['id']; // you need to receive the param from the url
Solution 2: If you want to keep the <form>, you should write the following code:
<?php
echo '<td><form method="POST" action="editRecord.php"> '.
'<input type="hidden" name="id" value="'.$id.'" />'.
'<input type="submit" name="Edit"></form></td>';
?>
And on page editRecord.php:
$id = $_POST['id']; // you need to receive the param from the url
I want to show the query result in the input type="text"but for some reason the text is blank. I only have one result row so maybe I don't need the while statement. But yet I can't make it to show in the input type text. Can you help me a see if this method is okay our not? Thanks in advance
<label for="nomegrupo"><b>Editar nome do grupo 1 :</label</b><br>
<?php
while ($row = mysqli_fetch_array($result6)){
?>
<tr>
<td><input type="text" value="<?php echo $row['titulogrupo']; ?>" name="grupo1" id="velhas"></td>
</tr>
<?php } ?>
<input type="submit" name="submit_x" data-inline="true" value="Submeter">
</form>
If you are certain you are only getting 1 result you can try this:
<label for="nomegrupo"><b>Editar nome do grupo 1 :</label</b><br>
<?php
$row = mysqli_fetch_array($result6)
if $row['titulogrupo']!='' { ?>
<tr>
<td><input type="text" value="<?php echo $row['titulogrupo']; ?>" name="grupo1" id="velhas"></td>
</tr>
<?php } ?>
<input type="submit" name="submit_x" data-inline="true" value="Submeter">
</form>
I am trying to GET the selected ID from the previous form and insert the id into another database. After inserting the data from the form the ID should not be cleared whereas other details can be cleared.
Here is the screenshot of from the form where ID has to be inserted
Screeshot
When the user clicks on Add image particular ID in which Add image is clicked as to be posted in another form.
After submitting the form the ID shouldnt be cleared. Whereas the other textbox can get cleared.
Here is the code
$query2 = "SELECT * FROM abc";
$result2=mysql_query($query2) or die("Query Failed : ".mysql_error());
if(isset($_POST['submit']))
{
$id= $_POST['id'];
if( ! ctype_alnum($id) )
die('invalid id');
$query = "SELECT id FROM `abc` WHERE `id` =$id";
$run = mysql_query($query);
if(mysql_num_rows($run)>0){
echo "<script>window.open('addimg.php?id=".$id."','_self')</script>";
}
else {
echo "<script>alert('No is Invalid!')</script>";
}
}
?>
<table width='300' align='center' border='1' cellpadding="5px" cellspacing="0px" style="color:#494949;font-size:12px; font-family:Cambria;">
<tr>
<td>ID</td>
<td>URl Small Image</td>
<td>URL Large Image</td>
<td>Add Image</td></tr>
<?php
while($row=mysql_fetch_array($result2))
{?>
<form method="post" action="report.php">
<tr><td>
<input type="text" name="id" class="input" size="20" value="<?php echo $row['id']; ?>" readonly></td>
<td>
<input type="text" name="s_img" class="input" size="20" value="<?php echo $row['s_img']; ?>" readonly></td>
<td>
<input type="text" name="l_img" class="input" size="20" value="<?php echo $row['l_img']; ?>" readonly></td>
<td> <input type="submit" name="submit" value="ADD IMAGE" class="button" /></td>
</form>
<?php
}
?>
Addimg.php
<form name="XIForm" id="XIForm" method="POST" action="addimg.php">
<h1>News Description</h1>
<p>
<label for="News_id" class="uname"> News_ID</label>
<input type="text" id="news_id" name="news_id" type="text" value="<?php if(isset($_GET['id'])) { echo $_GET['id']; } ?>" readonly> <br />
</p>
<label for="img1" class="uname"> Img1</label>
<input type="text" id="img1" name="img1" type="text"/> <br />
</p>
PHP code
if(isset($_POST['XIsubmit'])) {
$news_id = $_POST["news_id"];
$img1 = $_POST["img1"];
if($img1!="")
{
$query=("INSERT INTO images (news_id,img)VALUES('$news_id','$img1')") or die(mysql_error());
$retval = mysql_query( $query, $dbhandle );
echo ""
if(! $retval ) {
die('Could not enter data: ' . mysql_error());
}
}
}
?>
Good Evening, Everybody..
i am using php form to submit data of students in database using the below code...
<?php
if(isset($_POST['submit1']))
{
$a1=mysql_real_escape_string($_POST['a1']);
$a2=mysql_real_escape_string($_POST['a2']);
$a3=mysql_real_escape_string($_POST['a3']);
$a=mysql_real_escape_string($_POST['a']);
$a4=mysql_real_escape_string($_POST['a4']);
$a5=mysql_real_escape_string($_POST['a5']);
$a6=mysql_real_escape_string($_POST['a6']);
$color=mysql_real_escape_string($_POST['color']);
$date=mysql_real_escape_string($_POST['date']);
$query1=mysql_query("insert into student_table values('','$a1','$a2','$a3','$a','$a4','$a5','$a6','$color','$date')");
if($query1)
{
echo "Open Updated Successfully";
}
}
?>
<div class="RECORD" align="center"><font color="red" size="4">! STUDENT RESULTS !</font><div class="HEADLINES">! Live Result !<br>
<font color="yellow"><?php
$query1 = "SELECT CONCAT(a1,a2,a3,' - ',a,' - ',a4,a5,a6) FROM student_table ORDER BY id DESC
LIMIT 1;";
$result = mysql_query($query1);
$data = mysql_fetch_array($result);
echo $data[0];
?></font></div><hr>
<form method="post" action="" autocomplete="off">
DATE:<input type="text" name="date" size="6" align="absmiddle" value="<?php
$current_dayname = date("l");
echo $date = date("d/m/y",strtotime('monday this week')).' <br>to<br> '.date("d/m/y",strtotime("sunday this week"));
?>">
<br><br>Open:
<input type="text" name="a1" size="1" value="<?php echo $query2['a1']; ?>">
<input type="text" name="a2" size="1" value="<?php echo $query2['a2']; ?>">
<input type="text" name="a3" size="1" value="<?php echo $query2['a3']; ?>"> -
<input type="text" name="a" size="2" value="<?php echo $query2['a']; ?>">
<input type="submit" name="submit1">
</form>
<?php
$query1 = "SELECT id FROM student_table ORDER BY id DESC
LIMIT 1;";
$result = mysql_query($query1);
$data = mysql_fetch_array($result);
?>
Now By using the above code it shows desired result....
but i want to add one more result along with previous result, due to this i have to required edit page of above result, the desired code of edit page is below...
<?php
if(isset($_GET['id']))
{
$id=$_GET['id'];
if(isset($_POST['submit2']))
{
$a1=$_POST['a1'];
$a2=$_POST['a2'];
$a3=$_POST['a3'];
$a=$_POST['a'];
$a4=$_POST['a4'];
$a5=$_POST['a5'];
$a6=$_POST['a6'];
$color=$_POST['color'];
$date=$_POST['date'];
$query3=mysql_query("update student_table set a1='$a1', a2='$a2', a3='$a3', a='$a', a4='$a4', a5='$a5', a6='$a6', color='$color', date='$date' where id='$id'");
if($query3)
{
echo "</br> Close Updated Successfully";
}
}
$query1=mysql_query("select * from student_table where id='$id'");
$query2=mysql_fetch_array($query1);
?>
<form method="post" action="" autocomplete="off">
<input type="text" name="a4" size="1" value="<?php echo $query2['a4']; ?>">
<input type="text" name="a5" size="1" value="<?php echo $query2['a5']; ?>">
<input type="text" name="a6" size="1" value="<?php echo $query2['a6']; ?>">
<input type="text" name="a" size="2" value="<?php echo $query2['a']; ?>"><select type="text" name="color" value="<?php echo $query2['color']; ?>"> <option value="#000000">Black</option><option value="#FF0000">Red</option></select>
<input type="submit" name="submit2"><br>
<div style="background:black; color:black; font-size:1px">DATE:<input type="text" name="date" size="5" align="absmiddle" value="<?php
$current_dayname = date("l");
echo $date = date("d/m/y",strtotime('monday this week')).' <br>to<br> '.date("d/m/y",strtotime("sunday this week"));
?>"><input type="text" name="a1" size="1" value="<?php echo $query2['a1']; ?>">
<input type="text" name="a2" size="1" value="<?php echo $query2['a2']; ?>">
<input type="text" name="a3" size="1" value="<?php echo $query2['a3']; ?>"></div>
</form>
<?php
}
?>
Now my question is that....is it possible to perform both submitting of students numbers and having an option to add one more same result with editable function of previous result entry on the same page...
please refer below image...
can anybody help me in order to perform both functioning on same page with editable option
<?php
include 'connection.php';
?>
<form action="update.php" method="post">
<?php
$grp=$_GET['group_num'];
$query = "SELECT * FROM lectures_table WHERE group_num=$grp";
$result = mysql_query($query);
while($row=mysql_fetch_array($result)){
</div>
<input type="hidden" name="group_num" value="<?php echo $grp; ?>" />
<input type="text" placeholder="First Lecture" name="day[]" value="<?php
echo $row['day']; ?>"></td>
<input type="text" placeholder="First Lecture" name="f[]" value="<?php
echo $row['first']; ?>"></td><br>
</tr>
<?php
} ?>
</tbody>
</table>
<input type="submit" class="btn btn-success" name="edit" value="save">
</form>
</div>
</div>
</div>
</div> ?>
</body>
</html>
update.php
<?php
include 'connection.php';
$i = 0;
while ($i < 5) {
// define each variable
$scoreaway = $_POST['f'][$i];
// do the update and print out some info just to provide some visual feedback
echo $query = "UPDATE lectures_table SET first='".$scoreaway."' WHERE
group_num=1";
mysql_query($query) or die ("Error in query: $query");
$i++;
}
mysql_close();
?>
The update query just updates the last row. What should I do I need it importantly.
When I submit my form all values take the value of last input ??
You need to change the form so that each row has a hidden input containing the ID of that row.
while($row=mysql_fetch_array($result)){ ?>
<tr>
<td><input type="hidden" name="id[]" value="<?php echo $row['id']; ?>" /></td>
<td><input type="text" placeholder="First Lecture" name="day[]" value="<?php echo $row['day']; ?>"></td>
<td><input type="text" placeholder="First Lecture" name="f[]" value="<?php echo $row['first']; ?>"></td>
</tr>
<?php }
Then use that ID in the UPDATE statement, not group_num.
foreach ($_POST['id'] as $i => $id) {
$scoreaway = $_POST['f'][$i];
$day = $_POST['day'][$i];
$query = "UPDATE lectures_table
SET first='$scoreaway', day = '$day'
WHERE id = $id";
mysql_query($query);
}
Also, you should switch from the obsolete mysql extension to mysqli or PDO, and use prepared statements. Your current code is vulnerable to SQL injection. See How can I prevent SQL injection in PHP?