I am trying to create a functioning search field using ProductID, then using this to display the row in the product table where the ProductID exists. The problem I am having is that if I have the code as follows: The submit button below executes the $deleteprod and $deltesale code which all it should (from my understanding) is submit the ProductID variable.
<form action="delete.php" method="post">
<input value="" placeholder="Product ID" name="ProductID" type="text"/> <br>
<input type="submit" onclick="" value="Search"/
</form>
<?php
$ProductID = $_POST['ProductID'];
$db = mysql_connect("localhost:3307", "root", "usbw");
$deleteprod = mysql_query("DELETE FROM gameshop.product WHERE (product.ProductID = '$ProductID')");
$deletesale = mysql_query("DELETE FROM gameshop.sale WHERE (sale.ProductID = '$ProductID')");
$deletebutton = "<button onclick=\"$deletesale; $deleteprod; location.href='database.php'\" style='width:200px; background-color:red;'>Delete Row</button>";
mysql_select_db("gameshop",$db);
$result = mysql_query("SELECT product.*, sale.Price
FROM gameshop.product, gameshop.sale
WHERE (product.ProductID = '$ProductID' AND sale.ProductID = '$ProductID')",$db);
if (!$result){
print mysql_rror();
}
elseif ($myrow = mysql_fetch_array($result)){
echo "<table style='text-align:center;' border=1>\n";
echo "<tr><td>Product ID</td><td>Product Name</td>", "<td>Publisher</td><td>Developer</td>", "<td>Release Date</td><td>Stock</td>", "<td>Console</td> <td>Rank</td><td>Price</td><td>Delete?</td></tr>\n";
do{
printf("<tr><td>%4d</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%1d</td><td>%s</td><td>%1d</td><td>%2d</td><td>%s</td></tr>\n", // %s are syntax (string)
$myrow["ProductID"], $myrow["ProductName"], $myrow["Publisher"], $myrow["Developer"], $myrow["ReleaseDate"], $myrow["Stock"], $myrow["Console"], $myrow["Rank"], $myrow["Price"], $deletebutton);
}
while ($myrow = mysql_fetch_array($result));
echo "</table>\n";
}
mysql_close($db);
?>
If I were to move the $db = MySQL_connect("localhost:3307", "root", "usbw"); to below the $delete variables, this problem does not occur and it creates the single row table. However when attempting to click on the $deletebutton, the code does not execute and no row is deleted.
What am I doing wrong?
the problem is, when you click the button you call JavaScript, not PHP.
I think you could solve it making another PHP page with your deletesale and deleteprod which takes Product_ID with post.
so this file would have a php script like this:
$ProductID = $_POST['ProductID'];
$db = mysql_connect("localhost:3307", "root", "usbw");
mysql_select_db("gameshop",$db);
$result = mysql_query("SELECT product.*, sale.Price
FROM gameshop.product, gameshop.sale
WHERE (product.ProductID = '$ProductID' AND sale.ProductID = '$ProductID')",$db);
if (!$result){
print mysql_error();
}
elseif ($myrow = mysql_fetch_array($result)){
echo "<table style='text-align:center;' border=1>\n";
echo "<tr><td>Product ID</td><td>Product Name</td>", "<td>Publisher</td> <td>Developer</td>", "<td>Release Date</td><td>Stock</td>", "<td>Console</td> <td>Rank</td><td>Price</td><td>Delete?</td></tr>\n";
do{
printf("<tr><td>%4d</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%1d</td><td>%s</td><td>%1d</td><td>%2d</td><td>",$myrow["ProductID"], $myrow["ProductName"], $myrow["Publisher"], $myrow["Developer"], $myrow["ReleaseDate"], $myrow["Stock"], $myrow["Console"], $myrow["Rank"], $myrow["Price"]);
printf "<form method=\"post\" action=\"delete_page.php\"><input type=\"text\" style=\"display:none\" name=\"ProductID\" value=\"$ProductID\"/><input type=\"submit\" value=\"delete product\"/></form>"
printf "</td></tr>"
}
while ($myrow = mysql_fetch_array($result));
echo "</table>\n";
}
mysql_close($db);
and then you would have delete_page.php with
$ProductID = $_POST['ProductID'];
$db = mysql_connect("localhost:3307", "root", "usbw");
mysql_select_db("gameshop",$db);
mysql_query("DELETE FROM gameshop.product WHERE (product.ProductID = '$ProductID')");
mysql_query("DELETE FROM gameshop.sale WHERE (sale.ProductID = '$ProductID')");
mysql_close($db);
then you can add a link back to the main page.
Be sure to check for typos :)
Related
I'm doing a table where the user can introduce the number of the products he want to buy and submit them pressing the submission button. I build the content of the table using a sql query (SELECT) setting the name of the input(text) like in the code below. My problem is that, probably, the form doesn't submit nothing because, if a check the variable $_REQUEST with isset(), it return FALSE. Someone can help me? Thanks!
$con=mysqli_connect("localhost","uPower","","info");
if (mysqli_connect_errno())
printf("<p>Error!!: %s<p>\n", mysqli_connect_error());
else{
$query = "SELECT * FROM product";
$result = mysqli_query ($con, $query);
echo "<form name=\"buy\" action=\"confirm.php\" method=\"POST\" onSubmit=\"return checkInputQty(qty.value) \">";
echo"<table>";
echo"<tr><th>Product name</th><th>Price</th><th>Available</th><th>Qty</th></tr>";
while ($row = mysqli_fetch_assoc($result)){
$pid = $row["pid"];
$name = $row["name"];
$qty = $row["qty"];
$price = ($row['price'])/100;
echo"<tr><th>$name</th><th>".number_format($price,2,',', '')." €</th><th>$qty</th><th><input type=\"text\" id=".$pid." name=".$name."></th></tr>";
}
echo"</table>";
echo "<p><input type=\"submit\" value=\"BUY\" class=\"button\" ><input type=\"button\" onclick=\"clearText()\" value=\"DELETE\" class=\"button\"></p>";
echo "</form>";
mysqli_free_result($result);
mysqli_close($con);
there is the part of confirm.php. It is a function in head that i called from the body of the page. It does a summary of what the user selected to buy. The control with if(isset($_REQUEST)) is false, so the page is blank....
<?php
function checkInputQty(){
$con=mysqli_connect("localhost","uPower","","info");
if (mysqli_connect_errno())
echo "error DB";
else{
$query = "SELECT * FROM product";
$result = mysqli_query ($con, $query);
while($row = mysqli_fetch_assoc($result)){
$nome = $row["name"];
if(isset($_REQUEST["$name"])){
$qty = trim($_REQUEST['$row["name"]']);
if($qty>0 && $qty<=$row["qty"]){
$new_qty = $row["qty"]-$qty;
$query2 = "UPDATE product SET qty = '$new_qty' WHERE qty = '".$row["qty"]."' ";
$result2 = mysqli_query ($con, $query2);
if($result){
echo "<table>";
echo "<tr><th>Name</th><th></th><th>Price</th></tr>";
do_content($row["name"],$qty,$row["price"]/100);
echo "</table>";
}
else
echo "Error!";
}
}e
}
mysqli_free_result($result);
}
mysqli_close($con);
}
?>
<?php
function do_content($name,$to_buy,$price){
echo "<tr><th>$name</th><th>$to_buy</th><th>$price</th></tr>";
}
?>
I am trying to remove a string from DB by a Href in a form. Why does this not work for me? The query is right, nothing happens.
<?php
// Make a MySQL Connection
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("cms") or die(mysql_error());
// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM pages")
or die(mysql_error());
echo "<table border='1'>";
while ($row = mysql_fetch_array($result)) {
echo "<li class='list-group-item'>";
echo $row['header'];
echo "<br/>";
echo $row['description'];
echo "<br/>";
echo "<form method='POST'><a href='delete.php?id=".$row['page_id']."'>Remove</a></form>";
echo "</li>";
}
echo "</table>";
?>
delete.php
<?php
$db = new mysqli('localhost', 'root', '', 'cms');
$id = mysqli_query($db, "SELECT page_id FROM pages");
if (isset($_POST['id'])) {
mysqli_query($db, "DELETE FROM pages WHERE id='".$_GET['id']."'");
}
?>
And it still won't work. Just sends me to a blank page with no query.
Change this :
echo "<form method='POST'><a href='delete.php?".$row['page_id']."'>Remove</a></form>";
to
echo "<form method='POST' action='delete.php'><input type='hidden' value=".$row['page_id']." name='id'/><input type='submit' value='Remove'/></form>";
And in delete.php
This :
if (isset($_GET['id'])) {
To
if (isset($_POST['id'])) {
This
mysqli_query($db, "DELETE FROM pages WHERE id='".$_GET['id']."'");
to
mysqli_query($db, "DELETE FROM pages WHERE id='".$_POST['id']."'");
I have a large database of venues - and I would like to display this data in one page that would only change in some sort of an attribute to the id: (Ex: venues.php?id=1, which would get all the data from row #1.)
Edit: Okay, I updated the code and this is what it looks like now:
<?php
mysql_connect("localhost", "", "") or die(mysql_error());
mysql_select_db("") or die(mysql_error());
$id = (int) $_GET['id'];
$data = mysql_query("SELECT * FROM venues WHERE id = ".(int)$id) ;
or die(mysql_error());
Print "<table border cellpadding=3>";
while($info = mysql_fetch_array( $data ))
{
Print "<tr>";
Print "<th>Name:</th> <td>".$info['VENUE_NAME'] . "</td> ";
Print "<th>Address:</th> <td>".$info['ADDRESS'] . " </td></tr>";
}
Print "</table>";
?>
And upon going to venues.php?id=1 I get this error:
Parse error: syntax error, unexpected T_LOGICAL_OR in
/home/nightl7/public_html/demos/venues/venues.php on line 8
Do you mean something like:
$id = (int) $_GET['id'];
$data = mysql_query("SELECT * FROM venues WHERE id = ".(int)$id) ;
In order to "pass" the id into your url "venues.php?id=1"
You need to use a hybrid html/php form with method=get.
You can see an example html form here: w3schools html forms
This is what I would do:
print '<form name="input" action="venues.php" method="get">';
print 'Venue: <select name = "id">';
$con = mysql_connect("","","");
mysql_select_db($dataBase);
if (!$con){die('Could not connect: ' . mysql_error());}
else {
$opt = array();
$optVal = array();
$i = 0;
$sql = "Select * from venues";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result))
{
$opt[$i] = $row['VenueName'];
$optVal[$i] = $row['VenueID'];
print "<option value='$optVal[$i]'>$opt[$i]</option>";
$i++;
}
}
mysql_close($con);
print '</select><br />';
print '<input type="submit" value="Submit" />';
print '</form>'
This will give you a form that will give you a drop down list of all your venues and once a venue is selected will direct you to the venues.php page with the respective id.
at the top of your venues,php page just use
$id = $_GET['id'];
This assigns the id number to the variable $id and then you can use this "select"
$data = mysql_query("SELECT * FROM venues WHERE id = ".$id) ;
To collect your venue name from your database using the id supplied in the form.
Good Luck :)
<?php
mysql_connect("localhost", "", "") or die(mysql_error());
mysql_select_db("") or die(mysql_error());
$id = (int) $_GET['id'];
$data = mysql_query("SELECT * FROM venues WHERE id = ".$id) or die(mysql_error());
Print "<table border cellpadding=3>";
while($info = mysql_fetch_array( $data ))
{
Print "<tr>";
Print "<th>Name:</th> <td>".$info['VENUE_NAME'] . "</td> ";
Print "<th>Address:</th> <td>".$info['ADDRESS'] . " </td></tr>";
}
Print "</table>";
?>
everytime i try and add one to the second column of a certain name, it changes the value to 5, if i echo my event it says it is equal to resource id #4. Anyone have any fixes?
<form action="new.php" method="POST">
<input type="text" name="input_value">
<br />
<input name="new_User" type="submit" value="Add to Users">
<input type="submit" name="event_Up" value="Attended Event">
<?php
//Connect to Database
mysql_connect("localhost", "root", "");
//If Add New user butten is clicked execute
if (isset($_POST['new_User']))
{
$username = $_POST['input_value'];
$make = "INSERT INTO `my_db`.`profile` (`Name`, `Events`) VALUES ('$username', '1')";
mysql_query($make);
}
//If Event up is pushed then add one
if (isset($_POST['event_Up']))
{
$username = $_POST['input_value'];
$event = mysql_query("SELECT 'Events' FROM `my_db`.`profile` WHERE Name ='$username'");
$newEvent = $event +1;
$update = "UPDATE `my_db`.`profile` SET Events = '$newEvent' WHERE Name = '$username'";
mysql_query($update);
}
//Print Table
$data = mysql_query("SELECT * FROM `my_db`.`profile`");
Print "<table border cellpadding=4>";
while($info = mysql_fetch_array($data))
{
Print "<tr>";
Print "<th>Name:</th> <td> ".$info['Name'] . "</td>";
Print "<th>Events:</th> <td>".$info['Events'] . " </td>";
}
Print "</table>";
?>
I've cleaned up your code a little bit.
It's still a mess, but should at least work (un-tested though).
<form action="new.php" method="post">
<input type="text" name="input_value">
<br />
<input name="new_User" type="submit" value="Add to Users">
<input type="submit" name="event_Up" value="Attended Event">
</form>
<?php
//Connect to Database
mysql_connect("localhost", "root", "");
//If Add New user butten is clicked execute
if (isset($_POST['new_User']))
{
$username = empty($_POST['input_value']) ? NULL : $_POST['input_value'];
if ( ! empty($username))
{
mysql_query("
INSERT INTO `my_db`.`profile`
(`Name`, `Events`)
VALUES
('". mysql_real_escape_string($username) ."', 1)
");
}
}
//If Event up is pushed then add one
if (isset($_POST['event_Up']))
{
$username = empty($_POST['input_value']) ? NULL : $_POST['input_value'];
if ( ! empty($username))
{
$event = mysql_query("
SELECT
Events
FROM
`my_db`.`profile`
WHERE
Name = '". mysql_real_escape_string($username) ."'
");
$newEvent = (int) (mysql_result($event, 0, 'Events') + 1);
mysql_query("
UPDATE
`my_db`.`profile`
SET
Events = $newEvent
WHERE
Name = '". mysql_real_escape_string($username) ."'
");
}
}
//Print Table
$data = mysql_query("SELECT * FROM `my_db`.`profile`");
Print "<table border cellpadding=4>";
while($info = mysql_fetch_assoc($data))
{
Print "<tr>";
Print "<th>Name:</th> <td> ". htmlentities($info['Name'], ENT_COMPAT, 'UTF-8') . "</td>";
Print "<th>Events:</th> <td>". htmlentities($info['Events'], ENT_COMPAT, 'UTF-8') . " </td>";
}
Print "</table>";
?>
Edit:
Just so you are aware... your issue was $newEvent = $event +1;.
$event is a MySQL resource, not the query's result. You have to use one of the mysql_* functions to get the data (see my code above.)
It seems you are just learning PHP, and I would highly recommend you stop using the mysql_* functions right now and start using PDO.
use mysql_fetch_assoc not mysql_fetch_array
any time you get a resource id rather than data it means you have just a pointer to something and most likely need a function call to get the data out.
You need to fetch the array and then define $event based on the results. You're assigning $events on the mysql query itself.
$result = mysql_query("SELECT 'Events' FROM `my_db`.`profile` WHERE Name ='$username'");
while($row = mysql_fetch_array( $result )) {
$event = $row['Events'];
}
I have retrieved the database details from a database to a
php page. i have actually retrieved a specific column of a query.
but i am not able to add the radio buttons to the retrieved values.
Following is my coding:
<?php
$query = "SELECT url FROM measurementurl";
$result = mysql_query($query);
while($row = mysql_fetch_row($result))
{
$url = $row[0];
echo "url :$url <br>" ;
}
?>
Try this:
<form action="">
<?php
$query = "SELECT url FROM measurementurl";
$result = mysql_query($query);
while($row = mysql_fetch_row($result))
{
//$url = $row[0]; removed cause not used in code
echo "<input type=\"radio\" name=\"url\" value=\"$row[0]\" />$row[0]<br />";
}
?>
</form>