I've been having a rather irritating issue regarding capturing SQL information and then placing it into a PHP form (in theory, it should be kinda easy).
Here's the code for the SQL database information:
<?
$select = "SELECT * FROM beer WHERE country_id = 3";
$data = mysql_query($select) or die("Unable to connect to database.");
while($info = mysql_fetch_array($data)) {
echo '<center>';
echo '<h2>'.$info['name'].'</h2>';
echo '<table style="padding:0px;"><tr>';
echo '<tr><td><b>ABV%:</b></td><td width="570">'.$info['abv'].'</td></tr>';
echo '<tr><td><b>Bottle Size:</b></td><td width="570">'.$info['bottleSize'].'</td></tr>';
echo '<tr><td><b>Case Size:</b></td><td width="570">'.$info['caseSize'].'</td></tr>';
echo '<tr><td><b>Price:</b></td><td width="570">$'.$info['price'].'</td>';
echo '</tr></table>';
echo '</center>';
echo '<br/>';
echo '<img src="" border="0"><br><br>';
echo '<form name="cart" method="post" action="cart.php"> <table border="0"> <tr>';
echo '<td><input type="hidden" name="bname" value="'.$info['name'].'"><input type="hidden" name="price" value="'.$info['price'].'"></td>';
echo '<td><b>Quantity:</b></td>';
echo '<td><input type="text" name="qty" size="3"></td>';
echo '<td><input type="submit" value="Add to Cart" a href="cart.php?name=foo&price=bar" /a></td>';
echo '</tr></table></form>';
}
?>
I want when the submit value is pressed to somehow transmit the price, quantity and name to a basic HTML form (so that all the user has to do is add name, address, etcetc). I am completely stumped on how to do this.
If anyone could help, it would be much appreciated.
As you mentioned Amazon checkout, here is one thing you probably don't understand.
Amazoin doesn't use the form to move items data between server and browser to and fro.
It is stored in a session on a server time. All you need is some identifier put into hidden field.
To use a session in PHP you need only 2 things:
call session_start() function before any output to the browser on the each paghe where session needed.
Use `$_SESSION variable.
That's all.
Say, page1.php
<?
session_start();
$_SESSION['var'] = value;
and page2.php
<?
session_start();
echo $_SESSION['var'];
You wrote that code? because it's simply the same code as here.
You'll need to write an HTML form in your cart.php file
and use the $_POST variable to show the values of the price , quanitity and name.
For example:
<form method='post'>
<input type='text' name='price' value='<?=$_POST['price']?>'>
<input type='text' name='quanitity' value='<?=$_POST['qty']?>'>
Related
I have two .php pages, one that returns the contents of a table including an id for each entry along with an html text input and another that retrieves the details and allows me to update the record.
I'd like to store the id of the entry by clicking on the list item using a href rather than having to text input the id and submit.
choose.php
echo "<ul>";
while ($row=mysql_fetch_array($result)) {
$reference=$row[reference];
$name=$row[name];
echo "<li>$reference, $name</li>";
}
echo "</ul>";
session_start();
$_SESSION['regName'] = $reference;
mysql_close($link);
?>
<form method="get" action="update.php">
<input type="text" name="regName" value="">
<input type="submit">
</form>
update.php
session_start();
$reference = $_GET['regName'];
echo "Your selection id is: ".$reference.".";
$query="SELECT * FROM firsttable WHERE reference='$reference'";
$result=mysql_query($query) or die("Query to get data from firsttable failed with this error: ".mysql_error());
$row=mysql_fetch_array($result);
$name=$row[name];
echo "<form method=\"POST\" action=\"updated.php\">";
echo "<p>";
echo "<label for=\"name\">Name: </label><input type=\"text\" id=\"name\" name=\"name\" size=\"30\" value=\"$name\"/>";
echo "<p><input type=\"submit\"></p>";
echo "</form>";
I apologise if this seems very obvious, I've only started to learn php as of today and it's much more complicated than anything I've done up until now.
Thanks
James
Answering your question, you just need to use HREF parameter of A tag. This will make an active link, which will contain a reference you need:
echo '<ul>';
while ($row = mysql_fetch_array($result)) {
$reference = $row[reference];
$name = $row[name];
echo '<li><a href=/update.php?regName='.$reference.'>'.$name.'</a></li>';
}
echo '</ul>';
?>
Read here for more details about passing data via GET\POST: https://www.w3schools.com/tags/ref_httpmethods.asp
And as you were told above, please consider rewiring the code as it is totally unsafe and must not be used anyhow besides some very basic concept proof. And only inside intranet or local machine.
Good luck!
I am trying to pass a value that I have received from my database to another php page to use within another SQL statement.
I have tried using sessions and also passing using the $_POST method on the other page but have had no luck.
Here is a snippet of my code looping through to display all records:
while($row = mysqli_fetch_array($sql)){
echo '<td>
<img src='.'"'.$row['image'].'"'.'><br/>
<form method="post" action="edit-record.php">
<input type="text" name="imgID" value='.'"'.$row['id'].'"'.'>
<input type="submit" value="Edit" id="edit_btn" class="admin_btn"></form>
</td>';
}
The value that I need is the ID for each specific image - $row['id'].
When the user clicks the EDIT button, they should be redirected to another page which displays only the specific record. This is why I need the ID received passed to the next page to insert into a query statement.
I hope this made sense and any help will be greatly appreciated.
UPDATE: Thanks for all of your help. I solved the problem by playing around with a few of your suggestions to pass the id via GET in the action of the form.
<form method="post" action="edit-record.php?id='. $row['id'].'">
No idea why that hadn't occurred to me! Thanks again.
while($row = mysqli_fetch_array($sql)){
echo '<td>
<img src="'.$row['image'].'"><br/>
<form method="post" action="edit-record.php">
<input type="text" name="imgID" value="'.$row['id'].'">
<input type="submit" value="Edit" id="edit_btn" class="admin_btn">
</form>
</td>';
}
in edit-record.php...
<?php
echo $_POST['imgID'];
?>
There is no reason your code technically wouldn't work but instead you could just eliminate the form and use a simple link...
while($row = mysqli_fetch_array($sql)){
echo '<td>
<img src="'.$row['image'].'"><br/>
edit
</td>';
}
and in edit-record.php...
<?php
echo $_GET['id'];
?>
4 Ways to do this...
1) Use a cookie
2) Use a session (which by default uses a cookie but in a different way)
3) Use cURL
4) add it to the GET parameters... ie. somepage.com/page.php?id=1
Strange concatenation
<input type="text" name="imgID" value="'.$row['id'].'">
Sure you select id on the mysql query???
If you make
echo $_POST['imgID'];
what is the result???
You can pass the id via get in the action form:
<form method="post" action="edit-record.php?id='. $row['id'].' ">
On the other page you recive the form in $_POST and the id in $_GET['id']
~Aha, I think the problem is your quotes. Single quotes don't allow variables to be interpreted.~
Nevermind, thats not your problem, but I already wrote it out, so I'll leave it. Look how much cleaner those quotes are :)
Switch up your quotes like so:
echo "<td>
<img src='{$row['image']}'><br/>
<form method='post' action='edit-record.php'>
<input type='text' name='imgID' value='{$row['id']'}'>
<input type='submit' value='Edit' id='edit_btn' class='admin_btn'></form>
</td>";
Need curly braces around array element (e.g {$row['id']})
My website is featuring online classified advertisements, programmed by PHP and MySQL. The following code let the administrator delete multiple records using the checkbox tool.
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" >
<table>
<td><? echo $rows['CountryName']; ?></td>
<td><input name="delete_items[]" type="checkbox" value="<?php echo $rows['id']; ?>" /></td>
</table>
<input type="submit" name="deleteSelected" value="Submit" >
</form>
<?php
if(isset($_POST['deleteSelected'])) {
$delete_items = join(', ', $_POST["delete_items"]);
$query = "DELETE FROM $table_name WHERE id IN ($deleted_items)";
$result = mysql_query($query);
header("Location: admin.php"); }
?>
When I press the submit button without checking boxes (all boxes are unchecked), I receive the following error message (p.s. the script is working well without any error message, if any Checkbox being checked):
Warning: join() [function.join]: Invalid arguments passed in C:\xampp\htdocs\admin_listing.php on line 87
I’ve tried the implode function instead of using join, but still I'm getting an error message.
Maybe I do not passing an array through the function correctly, but I cannot find a solution for the above.
Any advise would be appreciated.
It looks like you are displaying all the records from your database into a single input in the form.
The code will probably work well with the implode() as you tried, but you will need to use a loop in the displaying of the form to generate it properly with the options.
Something like this:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" >
<table>
<?php
while($row=$databaseResult) //however you are getting the data out of the database.
{
echo "<tr><td>".$rows['CountryName']."</td><td><input name='delete_items[]' type='checkbox' value=".$rows['id']."/></td></tr>";
}
?>
</table>
<input type="submit" name="deleteSelected" value="Submit" >
</form>
Thank you all for trying to help, I found a simple solution by adding one code line, as follows:
<?php
if(isset($_POST['deleteSelected'])) {
if(isset($_POST["delete_items"][0])) {
$delete_items = join(', ', $_POST["delete_items"]);
$query = "DELETE FROM $table_name WHERE id IN ($delete_items)";
$result = mysql_query($query);
header("Location: admin.php");
}
}
?>
Hope it can help someone else...
please if you could help me with this, i have a problem with the php_self coding, this is my code:
This is the php code
<?php
if (isset($_POST['submit']))
{
$id=$_REQUEST['id'];
$notes_en=$_REQUEST['notes_en'];
$notes_ru=$_REQUEST['notes_ru'];
$sql="UPDATE hostess SET notes_en='$notes_en',notes_ru='$notes_ru' WHERE id='$id'";
$result=mysql_query($sql);
if($result){
echo "<div id='edittxt'>successfully updated";
echo "<BR>";
echo '<td >View Results</div>';
}
else {
die('error'.mysql_error());
}}
?>
This here is the form code from which i ake the information
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label>Notes</label></br><div id="note"><h1><textarea value="<?php echo $star['notes_en'];?>"><?php echo $star['notes_en'];?></textarea></h1></div></br>
<input name="id" type="hidden" id="id" value="<?php echo $star['id'];?>">
<input type="submit" value="submit" name="submit"></form>"
If possible please check it because i've been trying to modify it and i get no results, it doesn't give me errors but neither does it update the database
You don't have a name attribute on your field. Also, doesn't takes a value attribute.
Try this:
<textarea name="notes_en"><?php echo $star['notes_en'];?></textarea>
Furthermore, I definitely recommend following Marc B's advice and protecting yourself against SQL injection attacks. Have a look at this thread:
How can I prevent SQL injection in PHP?
First, thank you for reading this.
I am just starting php and I am tying to make a site using FileMaker to display and enter information.
I have the php connecting to my database, then a search page using a form, then it displays a list of records. I would like to make a "button" that will select one record then display related records.
This is where my trouble is. I do not know how to make a form that will save either the record_Id or key field to then display the next page.
I am using a foreach loop to display the list in a table:
$records = $result->getRecords();
echo '<table border="1">';
echo '<tr>';
echo '<th>Company</th>';
echo '<th>Id Num</th>';
echo '<th>Choose</th>';
echo '</tr>';
foreach ($records as $record) {
echo '<tr>';
echo '<td>'.$record->getField('Company').'</td>';
echo '<td>'.$record->getField('K_Medical').'</td>';
echo '<td>
<form action="welcome.php" method="post">
#This is where I think I need the button, but instead it just breaks :(
<input type="hidden" name="med_id[]" value='$record->getField('K_Medical')/>';
<input type="submit" />
</form>';
echo '</form></td>';
echo '</tr>';
}
echo '</table>';
As you can see I have tried to use a hidden form field to get the key field of the record, but the page dose not work. I get an error 500 when I try to view it in a browser.
Any help would be greatly appreciated! If I have not provided enough information please let me know.
Replace :
echo '<td>
<form action="welcome.php" method="post">
#This is where I think I need the button, but instead it just breaks :(
<input type="hidden" name="med_id[]" value='$record->getField('K_Medical')/>';
<input type="submit" />
</form>';
By :
echo '<td>
<form action="welcome.php" method="post">
#This is where I think I need the button, but instead it just breaks :(
<input type="hidden" name="med_id[]" value='.$record->getField('K_Medical').'/>
<input type="submit" />
</form>';
You have a quotes and concatenation errors.