PHP delete query does not send the id [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I tried to delete data out of PHPMyAdmin tables using PDO and it is not working. My connection is established. When I click on the delete button in the URL it would not pass in the ID. Any ideas how I can get it to work?
Here is the form:
<?php include_once 'admin/includes/helpers.inc.php';?>
<?php include 'index.php' ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>List of Jokes</title>
</head>
<body>
<p>Add your own joke</p>
<p>Here are all the jokes in the database</p>
<!-- into a table -->
<table border="1">
<?php foreach ($jokes as $joke): ?>
<form action="?deletejoke" method="POST">
<tr>
<td><?php html($joke['joketext']);?></td>
<td><?php $display_date = date("Y-m-d", strtotime($joke['jokedate']));
html($display_date); ?>
</td>
<td><img height="100px" src="../images/<?php html($joke['image']);?>" /></td>
<td><a href=" mailto:<?php html($joke['email']);?>">
<?php html($joke['name']);?></a></td>
</td>
<td><input type="hidden" name="id" value="<?php echo $joke['joke_id'];
?>">
<input type="submit" value="Delete"></td>
</tr>
</form>
<?php endforeach; ?>
</table>
<?php include 'admin/includes/footer.inc.html.php';?>
</body>
</html>
Here is my delete block:
if (isset($_POST['deletejoke'])) {
try
{
$sql = 'DELETE FROM joke WHERE joke_id = :id';
$s = $pdo->prepare($sql);
$s->bindValue(':id', $_POST['id']);
$s->execute();
} catch (PDOException $e) {
$error ='Error deleting joke' . $e->getMessage();
include 'error.html.pp';
exit();
}
header('Location: .');
exit();
}
Many thanks.

First of all you got an error in your code in your catch:
`include 'error.html.pp';`
should probably be:
include 'error.html.php';
Second thing is that you are not passing any value in $_POST['deletejoke'], as it is being sent as $_GET if being sent at all when it has no value (depends of the browser)...
So basically change this:
<form action="?deletejoke" method="POST">
to this:
<form action="?deletejoke=1" method="POST">
And...
if (isset($_POST['deletejoke'])) {
to:
if (isset($_GET['deletejoke'])) {

deletejoke is the name of the form which is not passed through to $_POST.
This line:
if (isset($_POST['deletejoke']))
Should be
if (isset($_POST['id']))

Related

question about execute PHP file, from other php file on the same folder [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 months ago.
Improve this question
i download 2 php file, one is index. php for input form, and other is insert.php. i have put them on the same folder.
when i click submit it execute insert.php file it work fine and it insert form data into my database. but the question is: how it is called? no code line on the index.php calling the insert.php file.
here is a code of index.php
<!DOCTYPE html>
<html lang="en">
<head>
<title>GFG- Store Data</title>
</head>
<body>
<center>
<h1>Storing Form data in Database</h1>
<form action="insert.php" method="post">
<p>
<label for="firstName">Name:</label>
<input type="text" name="name" id="name">
</p>
<p>
<label for="lastName">Branch:</label>
<input type="text" name="branch" id="branch">
</p>
<p>
<label for="Gender">Roll Number:</label>
<input type="text" name="roll_no" id="roll_no">
</p>
<input type="submit" value="Submit">
</form>
</center>
</body>
</html>
here is the code of insert.php
<!DOCTYPE html>
<html>
<head>
<title>TEST</title>
</head>
<body>
<center>
<?php
$conn = mysqli_connect("localhost", "root", "", "test");
// Check connection
if($conn === false){
die("ERROR: Could not connect. "
. mysqli_connect_error());
}
// Taking all 3 values from the form data(input)
$name = $_REQUEST['name'];
$branch = $_REQUEST['branch'];
$roll_no = $_REQUEST['roll_no'];
// Performing insert query execution
// here our table name is college
$sql = "INSERT INTO student VALUES ('$name','$branch','$roll_no')";
if(mysqli_query($conn, $sql)){
echo "<h3>data stored in a database successfully."
. " Please browse your localhost php my admin"
. " to view the updated data</h3>";
}
else{
echo "ERROR: Hush! Sorry $sql. "
. mysqli_error($conn);
}
// Close connection
mysqli_close($conn);
?>
</center>
</body>
</html>
insert.php is calling after clicking on submit button by "action" attribute which you have used in tag.
<form action="insert.php" method="post">
You can set another path as well as if you want to keep insert.php on any other folder location.

Values cannot be inserted into my database after choosing from drop down list submit [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am trying to insert the values from my form after clicking an option from my drop down list. However, it keeps telling me that my LINE 4 and LINE 5 of my submit.php has error. I do not know what is wrong with my $_POST statement.
Please enlighten me, I am very new to PHP and HTML.
Below is my code for my drop down list.
<!DOCTYPE HTML>
<html>
<head>
<title> Search by Development </title>
<meta http-equiv="Content-Type" content ="text/html; charset=iso-8859-1">
</head>
<body>
<form name="form1" action="submit.php" method="post">
<label type='text'> Name:</label>
<select name ='userID'>
<?php
$conn1 = new mysqli('localhost', 'root','','carpark_project');
$result1 = $conn1->query("select userid from user");
while($row =$result1->fetch_assoc())
{ ?>
<option value="<? php echo $row['userid']; ?>">
<?php echo $row['userid']; ?>
</option>
<?php
} ?>
</select>
<br>
<label type='text'> Development:</label>
<select name ='Development'>
<?php
$conn = new mysqli('localhost', 'root','','carpark_project');
$result = $conn->query("select development from carpark");
while($row =$result->fetch_assoc())
{ ?>
<option value="<? php echo $row['development']; ?>">
<?php echo $row['development']; ?>
</option>
<?php
} ?>
</select>
<br>
<input type="submit" name="submit" value="submit"/>
</form>
</body>
</html>
Below is the action PHP file.
<?php
$con = new mysqli('localhost','root','','carpark_project');
$development = $_POST['Development'];
$userid = $_POST['userid'];
$inserthistory = "Insert into history (userid, development) values ('$userid','$development')";
$result=mysqli_query($con,$inserthistory);
if($result)
{
header("refresh:5; url=history.php");
}
else
{
echo "Not Updated";
}
?>
Please help, thank you!
You have a typo. It should be $userid = $_POST['userID'];
Also use prepared statements here to prevent sql injection attacks
$inserthistory = "Insert into history (userid, development) values (? , ?)"; #create sql string with placeholders to prevent sql injection
$sql = $con->prepare($inserthistory); #prepare the query.this line returns true or false
$sql->bind_param('ss' , $userid, $development); #now specify that the variables are strings and then add the variables
if ($sql->execute() === true){ #execute it
#query successful
} else {
#error
echo $con->error;
}
Also just an important observation. You should have one file containing the database connection. Currently you are creating a new database connection for every form input which is bad.. Create a file and then establish the connection there. Then all you do is include that file where you need.
Change <select name ='userID'> to <select name ='userid'>

PHP online store, looping buttons & databases [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
So I am writing this code and I want to make it by getting the data of the database using a loop. The problem that I got is that I can't seem to find a way to push through a value to the database from the buttons that I got. I am open to ideas. Thanks !
<!Doctype HTML>
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css"/>
<title>Online Catalog</title>
</head>
<body>
<form action="" method="get">
<?php
session_start();
require('database.php');
echo "<h1>Catalog</h1>
<b id=".cart.">Show cart</b>";
if ($result = $connection->query("SELECT * FROM `store`")) {
$row_cnt = $result->num_rows;
while ($row = $result->fetch_assoc()) {
echo "<p>
<table>
<th>
<img src=".$row[img_path].'/'.$row[img_name].">
</th>
<td>
<h2>". $row[name] ."</h2><br>
". $row[description] ."<br><br>
Price : ". $row[price] ."$<br>
</td>
<td >
<input type='submit' name='add".$row[id]."' value='Add to cart'/>
</td>
</table>
</p>
";
if(isset($_GET['add.$row[id].'])){
$cart=1;
if(mysqli_query($connection, "INSERT INTO `store-project`.`store` (`incart`) VALUES ('$cart')")){
mysqli_close($connection);
header("Location: cart.php");
}
}
}
$result->close();
}
?>
</form>
</body>
</html>
Here is the database
And here is how it generally looks
try to use Post method in your form.
if($_POST['add.$row['id'].']){
$insert = "insert into store-project.store
set incart = '".$cart1."';
mysqli_query($insert);
header("Location: cart.php");
}
Hope it helps

$_POST Giving Empty values [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have an HTML Form and I am trying to get the values from the form in my php file.
<div id="qForm">
<form action="postComment.php" method="post" name="comment">
Name: <input type="text" name="askerName"><br>
Title: <input type="text" name="qTitle"><br>
<textarea maxlength="255" name="theQ"></textarea>
<input type="submit">
</form>
</div>
My PHP file looks like this but It echos as Welcome
<?php
$name = $_POST["askerName"];
echo $name; //this works
$textArea = $_POST["theQ"];
echo $textArea; //this does not work
?>
<html>
<head>
</head>
<body>
welcome <?php $_POST["askerName"]; ?>
</body>
</html>
Use
<body>
welcome <?php echo $_POST["askerName"]; ?>
</body>
instead.
you have to use print/echo to display your php output these two basic ways to get output: echo and print
<body>
welcome <?php print $_POST["askerName"]; ?>
</body>
else
<body>
welcome <?php echo $_POST["askerName"]; ?>
</body>
echo is marginally faster compared to print as echo does not return any value

Passing session variables from html form to multiple php page urls?

I'm trying to pass variables from the form to multiple pages in php
The form "rentcheck.php"
<?php require ("Connections/Project.php") ;?>
<?php session_start();?>
<title>Rent Check</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="rent.php">
<table width="385" height="70" border="1">
<tr>
<td><label for="select3">Select Customer</label>
<select name="Customer_ID" id="Customer_ID">
<?php
//Select from SQL Database Table (t_customer)
$sql=mysql_query("SELECT * from t_customer");
while ($Customer = mysql_fetch_array($sql)){
echo "<option value='".$Customer['Customer_ID']."'>".$Customer['Customer_Name']."</option>";
}
?>
</select></td>
</tr>
</table>
<p>
<input type="submit" name="button" id="button" value="Submit" />
</p>
</form>
</body>
</html>
After input and Pass it to page "rent.php"
<?php
require("Connections/Project.php");
//$_SESSION['yourvariable'] = 'foo';
//$newDate = date("d-m-Y", strtotime($row_Recordset1['Customer_CC_Exp_Date']));
session_start();
$datetoday=date("Y-m-d H:i:s");
$endOfCycle=date('Y-m-d', strtotime("+30 days"));
if(isset($_GET['page'])){
$pages=array("products","cart");
if(in_array($_GET['page'], $pages)){
$_page=$_GET['page'];
}else{
$_page="products";
}
}else{
$_page="products";
}
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Rent</title>
</head>
<body>
<p> <?php require($_page. ".php");?>
<?php echo $_POST['Customer_ID'];?></p>
</body>
</html>
This page (rent.php) shows the value from the form.
And the third page "products.php"
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<?php
if(isset($_GET['action']) && $_GET['action']=="add"){
$custget=$_SESSION['Customer_ID'];
$id=intval($_GET['id']);
if(isset($_SESSION['cart'][$id][$custget])){
$_SESSION['cart'][$id]['quantity']++;
$getcust=$_SESSION['Customer_ID'];
}else{
$sql_s="SELECT * FROM t_dvd_copy
WHERE dvd_copy_id={$id}";
$query_s=mysql_query($sql_s);
if(mysql_num_rows($query_s)!=0){
$row_s=mysql_fetch_array($query_s);
$_SESSION['cart'][$row_s['dvd_copy_id']]=array(
"quantity" =>1,
"price" => $row_s['price']
);
}else{
$message="NO";
}
}
}
?>
<?php if(isset($message)) {
echo"$message"; }
//echo print_r($_SESSION['cart']); ?>
<table width="489" height="52" border="1">
<tr>
<td width="123">DVD Copy ID</td>
<td width="120">Name</td>
<td width="91">Price</td>
<td width="127">Action</td>
</tr>
<?php
$sql="SELECT *, dvd_title FROM t_dvd_copy INNER JOIN t_dvd ORDER BY dvd_title ASC";
$query=mysql_query($sql);
while($row=mysql_fetch_array($query)) {
?>
<tr>
<td><?php echo $row['dvd_copy_id']?></td>
<td><?php echo $row['dvd_title']?></td>
<td><?php echo $row['price']?></td>
<td>Add To Cart</td>
<?php
}
?>
</table>
<body>
</body>
</html>
This page (products.php) shows:
Notice: Undefined index: Customer_ID in C:\xampp\htdocs\project3\rent.php on line 39" whenever I clicked the "Add to Cart" or manually type "rent.php?=cart".
I'm trying to do is to show(Customer_ID)/pass the variables on multiple pages("products.php","cart.php").
Any suggestions or ideas?
I think your problem is that you have not started the session on the other pages.
on every php page that you want to have a session you need to put session_start(); at the top.
if you don't the session will end and empty all the data from it.
if you want to make sure what is in your session you can print it out like so:
echo "<pre>";
echo print_r($_SESSION);
echo "</pre>";
Couple of things:
You mentioned that when you clicked "Add to Cart" it didn't work. I assume you mean the button labelled "Submit" on the rentcheck.php page. Can you confirm this is right please?
You would get an error when manually navigating to rent.php?=cart as you are looking for a Customer_ID key in the $_POST array but at this point you are not posting to the server, you are performing a $_GET request.
You have referenced $_SESSION['Customer_ID'] but I cannot see in your code where you have set this (products.php 7th line of code).
I would suggest thinking through how a user might navigate through your website:
where are they likely to start?
what variables will be available to you at this point?
what will you do if the required variables do not exist (e.g. $_SESSION)?
what is the earliest point you can collect the required information (your form)?
once the form has been completed correctly, how can I make sure that the required information is retained throughout the users session?
have I continued to test on each page that my required variables are available to me?
Start with that and see where it leads you.
Looking at the code I can see that you are including products.php inside rent.php
So a couple of thoughts here:
products.php should not have HTML <head> because it will be rendered
inside the <body>
If you have a variable available in rent.php it will be available
under the same name in products.php.
So if you have in rent.php:
$customer_id = $_POST['Customer_ID'];
require('products.php');
In products.php you can use $customer_id directly.

Categories