Still getting used to stackoverflow excuse my rookie-ness.. :)
Have an SQL query that returns a data put into a table in php. I want this table to be used for purchasing.
My idea was to use the product id, meaning i would use a dynamic php variable (not sure if I'm doing that right now) believe I saw a post something like $.$varaible$.$ it wasn't very clear and was a different subject.
My code is as follows:
$result = mysqli_query($con, "SELECT * FROM Product WHERE Type = 'Game'");
?>
<div class="wrapper">
<h1 class="headGame">Buy Some Games Man</h1>
</div>
<br />
<div class="wrapper">
<?php
echo
"<table border='1'>
<tr>
<th> Name </th>
<th> Picture </th>
<th> Console </th>
<th> Description </th>
<th> Price </th>
<th> Amount </th>
</tr>";
echo '<form id="gamesOrder" action="purchase.php">';
while($row = mysqli_fetch_array($result)) {
$id = $row['Pd_Key'];
echo"<tr>";
echo"<td>" . $row['Name'] . "</td>";
echo"<td>" . '<img class="prdPic" src="'. $row['Picture']. '">' . "</td>";
echo"<td>" . $row['Type2'] . "</td>";
echo"<td>" . $row['Description'] . "</td>";
echo"<td>" . $row['Price'] . "</td>";
echo"<td>" . '<input type="number" min="0" max="100"; name="'.$id.'" value=0>' . "</td>";
echo"</tr>";
}
echo '<input type="submit" value=" BUY ">';
echo '</form>';
?>
When I click the submit it changes the url but nothing happens, it doesn't redirect.
Any advice on how to get this entire process to work. The variable being used in a purchasing form, via a php file ie (purchase.php) and the variable to use for this.
EDIT - Had minor errors, but still not 100% on variable %id, won't that get redefined each loop, how can I have it dynamic so it can be used in a form to identify what the user wants to buy.
Now redirects but not to purchase.php
URL is ~/purchase.php?1=0&2=0&3=0&4=0&5=0&6=0&7=0&8=0&9=0&10=0&11=0&12=0&13=0&14=0&15=0&16=0&17=0&18=0&19=0&20=0&21=0
Thanks you legends you!! =D
You are missing the closing form caret:
echo '<form id="gamesOrder" action"purchase.php"';
should be:
echo '<form id="gamesOrder" action="purchase.php">';
Also you concatenation for ID is incorrect:
echo"<td>" . '<input type="number" min="0" max="100"; name=".$id." value=0>' . "</td>";
should just be:
echo"<td>" . '<input type="number" min="0" max="100"; name="id" value="' .$id. '" value=0></td>';
To access the id in purchase.php use the following code:
$id = isset($_GET['id']) ? $_GET['id'] : null;
And you need to assign the action to the form with an equals sign:
action="myaction.php"
And you don't pass the id right...
echo"<td>" . '<input type="number" min="0" max="100"; name=".$id." value=0>' . "</td>";
should be
echo"<td>" . '<input type="number" min="0" max="100"; name="'.$id.'" value=0>' . "</td>";
Ough, and on form you need to put action =
echo '<form id="gamesOrder" action="purchase.php">';
Ok, first you've got some HTML error, lets see:
In the Form element you need to close it and also include the method (as POST), see:
< form id="gamesOrder" action="purchase.php" method="POST">
To send data using a form you will need to include the data from the data base in form fields like this:
echo '< input type="text" name="myFieldName" value="'.$row['Price'].'">';
Any question let me know...
Cheers.
Related
I have displayed a table of my data from the data base with check boxes to the left. I want to find a way to link the check boxes to the question number (ID). when I hit submit I want the selected id's to be echoed. pretty much I want someone to be able to select the questions they want and then display them.
<?php
$con=mysqli_connect("####","####","#####","###");
$result = mysqli_query($con,"SELECT * FROM q_and_a ");
echo "<table border='1'>
<tr>
<th>Add</th>
<th>#</th>
<th>Question</th>
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
<th>Answer</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo '<td><input type="checkbox" name="questions[]" value="$id"></td>';
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['question'] . "</td>";
echo "<td>" . $row['choiceA'] . "</td>";
echo "<td>" . $row['choiceB'] . "</td>";
echo "<td>" . $row['choiceC'] . "</td>";
echo "<td>" . $row['choiceD'] . "</td>";
echo "<td>" . $row['answer'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
submit button
<form method="POST" action="makeTest.php">
<input type="submit" name="make" value="Make Test">
</form>
Make some edits to your code then it will work.
1. Change your query by adding fields not * (to ensure performance and display order)
$result = mysqli_query($con,"SELECT id,question,choiceA,choiceB,choiceC,choiceD,answer FROM q_and_a ");
then before while block open form tag(HTML)
<?php
//above codes will be there as you show before
echo '<form method="POST" action="makeTest.php">';
while($row = mysqli_fetch_array($result)){
{ $id=$row['id']; // initialize your id here, so as to pass it in checkbox too
// then continue with your code
}
?>
<input type="submit" name="make" value="Make Test">
</form>
in maketest.php yo can hande ckeckbox using foreach, see below
foreach($_POST['questions'] as $questions){
//do your job
}
I'm describing the correcting functioning of the process.
First the user posts an order, and the status initially is set to NOT APPROVED, but his manager is able to see the order request and can change the status to APPROVED, and then it is forwarded to the IT guy and he can change status to UNDER PROCUREMENT.
I am using radio buttons for gm and IT guy for changing the status. However, i am unable to change the status. Here's the code for better understanding
echo'<th>Product</th><th>Type</th><th>Quantity</th><th>Order Status</th>';
while ($row = mysqli_fetch_array($result)){
$type=$row["type"];
$make=$row["make"];
$quantity=$row["quantity"];
echo "<tr>";
echo "<td>" . $row['type'] . "</td>";
echo "<td>" . $row['make'] . "</td>";
echo "<td>" . $row['quantity'] . "</td>";
echo '<form method="post">';
//Radio buttons used here
echo '<td><input type="radio" name="status[' . $row['order_id'] . ']" value="not approved" checked >Not Approved<br>
<input type="radio" name="status[' . $row['order_id'] . ']" value="approved">Approved</td>';
echo '</form>';
$_SESSION['radio'] = $_POST['status[]'];
$qr = "UPDATE order_info SET status = '".$_SESSION['radio']."' WHERE username = '".$_SESSION['username']."'";
$rs = mysqli_query($con3,$qr) or die(mysqli_error($con3));
}
echo "</table>";
Also, I want the radio button to be checked by default according to the status of the order.
The problem is with your input names:
echo '<td><cb><input type="radio" name="status[' . $row['order_id'] . ']" value="not approved" checked >Not Approved<br>
<input type="radio" name="status[' . $row['order_id'] . ']" value="approved">Approved</cb></td>';
echo '</form>';
You're creating an array. You can see this with this example:
<form method ='post'>
<input type='radio' name='status["25"]' value='not approved'>Not Approved
<input type='radio' name='status[25]' value='approved'>Approved
<input type='submit' value='Submit'>
</form>
<pre>
<?
session_start();
print_r($_POST);
$_SESSION['radio'] = $_POST['status'];
print_r($_SESSION);
?>
</pre>
This may have been to keep the order_id, but your update query is not specifying an order_id and will update all rows for that username. If you're only updating one row at a time, you can pass the order id with a hidden field (if you even need it?):
echo '<input type="hidden" name="order_id" value="'.$row['order_id'].'">';
Which means you're probably missing an "and order_id =" qualifier in your update.
Thanks in advance for any light shed.
I have a mysql database consisting of customers with some fields pertaining to each customer. currently running on one of my lamp servers. There is security risks with my code at the moment, but I plan to get the functionality i'm looking for and then reconfigure the code for a tighter security. At the moment I have an html index file that calls on php script to search mysql database by firstname or lastname. Upon this query it displays a list of users and allows me to modify the user. When I click modify it pulls the correct customer id number, but it is not displaying any current information, nor allowing me to update the info.
To summarize, I would like to search a customer, and it pull up selected fields and show the content and allow me to actively change the data and resend it to the database.
My search.html code:
<html>
<body>
<form action="scripts/search.php" method="post">
Firstname: <input type="text" name="firstname">
<input type="submit">
</form>
<form action="scripts/lastnamesearch.php" method="post">
Lastname: <input type="text" name="lastname">
<input type="submit">
</form>
<form action="scripts/phonenumbersearch.php" method="post">
Phone Number: <input type="text" name="phone">
<input type="submit">
</form>
</body>
</html>
MY search.PHP Script:
//this script allows me to search the database by filling out one of the forms and clicking submit. Each of the forms calls upon it's own individual script, I realize that this is probably cumbersome, due to my lack of coding knowledge.
<?php
$con=mysqli_connect("localhost","root","*****","*******");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM customers WHERE `firstname` LIKE '$_POST[firstname]'");
echo "<table border='1'>
<tr>
<th>id</th>
<th>firstname</th>
<th>lastname</th>
<th>phone</th>
<th>address</th>
<th>notes</th>
<th>additional notes</th>
<th>passwords</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['phone'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";
echo "<td>" . $row['addnotes'] . "</td>";
echo "<td>" . $row['passwords'] . "</td>";
echo "Modify User";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
My modify.php script:
//this is where I believe one of my problems lie. when I click modify user on the search.php script it calls on this script and it loads the correct user/customer id in the address bar, but it doesn't show any existing data, nor does it update the data that I fill in the cells.
<?php
$con=mysqli_connect("localhost","root","crapola1","Computition");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$mysqli_query = "SELECT * FROM customers WHERE ID = $_get[id]";
$mysqli_result = mysqli_query($mysqli_query);
$customer = mysqli_fetch_array($mysqli_result);
?>
<h1> You are modifying a user</h1>
<form action="<?php echo $SERVER['PHP_SELF']; ?>" method="post">
Firstname<input type="text" name="inputFirstname" value="<?php echo $row['firstname']; ?>" /><br />
Notes<input type="text" name="inputNotes" value="<?php echo $row['notes']; ?>" />
<br />
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" />
<input type="submit" name="submit" value="Modify" />
</form>
Thanks again,
I've been searching on this topic for about a week now and have pieced together this much, but can't seem to get over this "hump"
$_GET is a super global array . It should be in UPPERCASE.
Change the query on your modify.php here
SELECT * FROM customers WHERE ID = $_get[id] to upper case.
Must be..
SELECT * FROM customers WHERE ID = ".$_GET['id']
Also, It is strictly not advised to pass the $_GET or $_POST parameters directly to your query as it leads to SQL injection. You need to switch over to PreparedStatements
I'm having an issue trying to update multiple entries in my database via a php populated drop down menu. Here is the code on my page that populates the table showing me all entries currently in my database:
$result = mysqli_query($con,"SELECT * FROM Submissions");
echo "<table border='1'>
<tr>
<th>First name</th>
<th>Last name</th>
<th>Email</th>
<th>Title</th>
<th>Text</th>
<th>Public Post OK?</th>
<th>Date/Time Submitted</th>
<th>Approved?</th>
<th>Test Approved</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['fname'] . "</td>";
echo "<td>" . $row['lname'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . nl2br($row['text']) . "</td>";
echo "<td>" . $row['publicpost'] . "</td>";
echo "<td>" . $row['time'] . "</td>";
echo "<td><select name=\"approved\"><option value=\"" . $row['approved'] . "\">" . $row['approved'] . "</option><option value=\"yes\">Yes</option><option value=\"no\">No Again</option></select></td>";
echo "<td>" . $row['approved'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
<br><br>
<form action="update.php" method="post">
<input type="submit" name="SubmitButton" value="Update" class="submit" style="cursor:pointer;">
</form>
<?php
mysqli_close($con);
?>
This is the php code for "update.php":
$approved = $_POST['approved'];
mysqli_query($con,"UPDATE Submissions SET approved = $approved");
$update_query= "UPDATE Submissions SET approved = '$approved'";
if(mysqli_query($con,$update_query)){
echo "updated";}
else {
echo "fail";}
?>
<form action="approvesubmissions.php">
<input type="submit" value="Approve Submissions page">
</form>
The goal is to have the ability to update the field "approved" with a drop down menu from "NO" to "YES" or vice versa. Instead, what is happening with this query, is that it is erasing the data in the "approved" field instead of updating it. I'm somewhat new to php and i have researched a TON on this and have come up with no solutions. Any help is GREATLY appreciated!
First, let's assume 'approved' is a TINYINT(1) or something.
Your select html should be more like this. It will autofill based on the DB value.
$selected = 'selected="selected"'; // pre-selection attribute
$isApproved = !!$row['approved']; // is this active? (approved is 1 or 0)
echo '<select name="approved">
<option value="1" ' . ($isApproved ? $selected : '') . '>Yes</option>
<option value="0" ' . (!$isApproved ? $selected : ''). '>No</option>
</select>';
Secondly, your form is at the bottom of the table, but your input that you want is in the table. When you submit your form, there is no $_POST['approved'], because that's technically not in a form. To fix, you'll need to put your opening form tag at the top before the table. Then, you'll want to put your submit button and closing form tag at the end, after you've echoed the table out.
Thirdly, your post.php page should NOT ever take user input directly into a query. But, simply do this:
// Convert input to boolean answer, then int (for the query).
$approved = isset($_POST['approved']) ? (int)!!$_POST['approved'] : 0;
mysqli_query($con,"UPDATE Submissions SET approved = '$approved'");
While we're on the topic, this would be a great time to jump into prepared statements for your project. It might sound scary, but it can save you from SQL injection.
// Make the prepared statement
$query = mysqli_prepare("UPDATE Submissions SET approved = ?");
// Safely bind your params
mysqli_stmt_bind_param($query, "i", $approved);
// Run it
mysqli_stmt_execute($query);
// "close" the statement (hint: it's reusable for things like bulk updates, etc)
mysqli_stmt_close($query);
Ive got to make a web-based checkout for an assignment and have come across a problem, ive imported a database and set up the data in a table with added check boxes alongside it. I need to take the reference number (stored first in the array) onto another page using sessions. From using var_dump i cant seem to get anything from the selected from the table.
Code:
Button code
<p><tr>
<input type="submit" name="Select" id="Select" value="Add Selected To Cart"/>
</tr></p>
Access database code(values changed for saftey)
<?php
Accesses database
$con=pg_connect("host=hostname port=portnumbers
dbname=name user=user password=password");
if (!$con)
{
die('Could not connect to database');
}
?>
Database display
//Creates table
echo "<table border='1'>\n<thead>\n<tr>\n";
echo "<th>Title</th>
<th>Platform</th>
<th>Description</th>
<th>Price</th>
<th>Buy</th>\n";
while($row = pg_fetch_array($res)){
echo"<tr>";
echo "<td>" . $row['1'] . "</td>";
echo "<td>" . $row['2'] . "</td>";
echo "<td>" . $row['3'] . "</td>";
echo "<td>" . $row['4'] . "</td>";
echo '<td><input type="checkbox" name="selected[]" value="' . $row['0'] . '" /></td>';
echo"</tr>";
}
echo"</table>";
?>
Well, I think you can as follow because you are trying to improve a shopping car:
//Use the `$_SESSION` var to hold the values
foreach ($_POST['selected'] as $item)
$_SESSION['cart'][$item] = $item;
As the item's id will be indexed, each time a submit is performed:
If the item exists, will be replaced
If the item does not exist, will be added
In order to remove items, you should use a "view cart" page and then show the items:
foreach ($_SESSION['cart'] as $item)
echo '' . $item . '';