How to remove row from mysqli table from a post href - php

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']."'");

Related

Declaring Variable Runs Variable

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 :)

When I display this page the PHP part of the code is not working. It will not display the topics

Code will not display topics from database. I just get a blank pages.
Any solutions?
The pages loads but it will not display any thing. They want me to add more context but it breaks it so here you go.
<?php
//Database stuff.
include_once("connect.php");
if ($conn->connect_error) {
trigger_error('Database connection failed: ' . $conn->connect_error, E_USER_ERROR);
}else{
mysqli_select_db($conn,"2159928_db");
}
$tid = '';
$cid = $_GET['cid'];
$tid = $_GET['tid'];
$sql = "SELECT * FROM topics WHERE category_id='".$cid."' AND id='".$tid."' LIMIT 1";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) == 1){
echo "<table width ='75%'>";
if (isset($_SESSION['uid'])) {
//echo "<form action ='post_reply.php?cid=".$cid." &tid =".$tid. "' method = 'post'>
//<input type = 'submit' name = 'submit' value = 'Reply'/>";
//echo "<tr><td colspan ='2'><center><input type='submit' value='Reply' onClick = 'window.open = 'post_reply.php?cid=".$cid." &tid =".$tid."' />";
echo "<tr><td colspan ='2'><center><input type='submit' value='Reply' onClick='window.open(\"post_reply.php?cid=$cid&tid=$tid\")' />";
} else {
echo "<tr><td colspan = '2'><p><center> Please login to reply to topics.</p></td></tr>";
}
//Trying to display this. Doesn't even display border box.
while ($row = mysqli_fetch_assoc($result)) {
$sql2 = "SELECT * FROM posts WHERE category_id='".$cid."' AND topic_id='".$tid. "'";
$result2 = mysqli_query($conn, $sql2);
while ($row2 = mysqli_fetch_assoc($result2)){
echo "<tr><td valign ='top' style = 'border: 5px solid #ffffff;'><div style = 'min-height: 125px;'>".$row['topic_title']."<br/>
by ".$row2['post_username']. " - " .$row2['post_date']. "<hr/>".$row2['post_content']."</div></td>";
}
//This part not relevant.
$old_views = $row['topic_views'];
$new_views = $old_views + 1;
$sql3 = "UPDATE topics SET topic_views='".$new_views."' WHERE category_id='".$cid."' AND id ='".$tid."' LIMIT 1";
$result3 = mysqli_query($conn, $sql3);
}
echo "</table>";
} else {
echo "<p>This topic does not exist.</p>";
}
?>
Try echo-ing out your $sql to see if the query is correct.
If query is correct. Try var_dump to see if there are any results.

My form doesn't submit information in input text

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,',', '')." &#8364</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>";
}
?>

Create hyperlink on table result and fill editable form in other page

I'm having problems to find how to create an hyperlink in a table column result and then, on click, open another page with all fields (textboxes) filled. Imagine when a click an ID, i do a select * from table where column_id = ID... Is there a way to do it?
Thanks.
Best regards
I'm not completely sure what you are asking, but this may help you a bit.
First make a Javascript.
<script type="text/JavaScript">
function selectID() {
var ID = document.getElementById("ID").value;
document.location.href ="yoursite.php?ID="+ID;
}
</script>
Then connect to your database to query the table for a link ID (or more) for example by changing the variable $value.
<?php
//Connect to database
mysql_connect("host", "user", "pass");
mysql_select_db("db_name");
$value = 'something';
$ID = $_GET['ID'];
if (!$ID) {
$ID = 0;
}
if ($ID == 0) {
$query = "SELECT * FROM table WHERE `column_1` = '$value'";
$result = mysql_query($query);
echo "<table>";
while($myrow = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>";
echo "ID";
echo "</td>";
echo "</tr>";
}
echo "</table>";
}
elseif ($ID > 0) {
$query2 = "SELECT * FROM table WHERE `column_id` = '$ID'";
$result2 = mysql_query($query2);
while($myrow2 = mysql_fetch_array($result2)) {
$value1 = $myrow2['column_1'];
$value2 = $myrow2['column_2'];
}
echo "<form type=\"GET\" action=\"$PHP_SELF\">";
echo "<input type=\"text\" id=\"ID\" name=\"ID\" value=\"$ID\"><br>";
echo "<input type=\"text\" id=\"value1\" name=\"value1\" value=\"$value1\"><br>";
echo "<input type=\"text\" id=\"value2\" name=\"value2\" value=\"$value2\"><br>";
echo "<input type=\"hidden\" id=\"search\" name=\"search\" value=\"searching\">";
echo "<input type=\"submit\" id=\"submitbutton\" name=\"submitbutton\" value=\" Search \">";
echo "</form>";
}
?>

Update in fetch array

Is it possible to execute an UPDATE in a mysql_fetch_array() loop? Like this:
$query = "SELECT * FROM inbox";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result) or die(mysql_error())){
echo "<div>";
echo "<form method='POST'>";
echo "<h1>".$row['link']."</h1>";
echo "<h3>".$row['tittle']."</h3> na";
echo "<input type='text' name='tittle'>";
echo "<h3>".$row['content']."</h3>";
echo "<textarea name='content'></textarea>";
echo "<input type='submit' name='".$row['link']."' value='Change'>";
echo "</form>";
echo "</div>";
$tit = $_POST['tittle'];
$ten = $_POST['content'];
$link = $row['link'];
if (isset($_POST[$link])) { mysql_query("UPDATE inbox SET tittle='$tit' content='$ten' WHERE link='$link'");}
}
It have to do update in mysql_fetch_array(), because I wanna to change content of that things.
You have an error in your syntax as the values should be , delimited:
if (isset($_POST[$link])) {
mysql_query("
UPDATE inbox SET tittle='$tit',
content='$ten'
WHERE link='$link'
");}
Note:
You should take a look at the mysqli class to handle your future queries. It's just as simple as your current method, but more secure and not deprecated.

Categories