How do I run multiple SQL Queries using "if(isset($_POST['Submit'])){" - php

Trying to make a CRUD, everything works except my Update function. I feel like the problem is in the second sql query. When I click on submit it just refreshes and the change is gone. Can anyone show me how to find what I need to change/show me what to change?
<head>
<title>Update</title>
</head>
<body>
</form>
<?php
require_once('dbconnect.php');
$id = $_GET['id'];
$sql = "SELECT * FROM dealers where ID=$id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<form action="" method="post">';
echo "Company: <input type=\"text\" name=\"CName\" value=\"".$row['CName']."\"></input>";
echo "<br>";
echo "Contact: <input type=\"text\" name=\"Contact\" value=\"".$row['Contact']."\"></input>";
echo "<br>";
echo "City: <input type=\"text\" name=\"City\" value=\"".$row['City']."\"></input>";
echo "<br>";
echo "<input type=\"Submit\" = \"Submit\" type = \"Submit\" id = \"Submit\" value = \"Submit\">";
echo "</form>";
}
echo "</table>";
} else {
echo "0 results";
}
if(isset($_POST['Submit'])){
$sql = "UPDATE dealers SET CName='$CName', Contact='$Contact', City='$City' where ID=$id";
$result = $conn->query($sql);
}
$conn->close();
?>

Instead of building a form inside PHP, just break with ending PHP tag inside your while loop and write your HTML in a clean way then start PHP again. So you don't make any mistake.
Also you've to submit your $id from your form too.
Try this
<?php
require_once('dbconnect.php');
$id = $_GET['id'];
$sql = "SELECT * FROM dealers where ID=$id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
?>
<form action="" method="post">
<input type="hidden" name="id" value="<?= $id ?>" />
Company: <input type="text" name="CName" value="<?= $row['CName'] ?>" />
<br>
Contact: <input type="text" name="Contact" value="<?= $row['Contact'] ?>" />
<br>
City: <input type="text" name="City" value="<?= $row['City'] ?>" />
<br>
<input type="Submit" name="Submit" id="Submit" value="Submit" />
</form>
<?php
} // end while loop
echo "</table>";
}
else {
echo "0 results";
}
Note: You are passing undefined variables into your update query. As you are submitting your form you must have to define those variables before you use them.
if (isset($_POST['Submit'])) {
$CName = $_POST['CName'];
$Contact = $_POST['Contact'];
$City = $_POST['City'];
$id = $_POST['id'];
$sql = "UPDATE dealers SET CName='$CName', Contact='$Contact', City='$City' where ID=$id";
$result = $conn->query($sql);
}
$conn->close();

that loop? ID primary key or not?
maybe u need create more key in table dealer like as_id
<input type="hidden" name="idform" value="$as_id">
in statment
if($_POST){
$idf = $_POST['idform'];
if(!empty($idf)){
$sql = "UPDATE dealers SET CName='$CName', Contact='$Contact', City='$City' where as_id=$idf";
$result = $conn->query($sql);
}
$conn->close();
}

Related

PHP - Undefined index : id - the value is not retrieved

Yes, I know there is a lot of 'Undefined index' questions floating around here and i have been looking through them before asking this question. I copied the codes from those questions to try and test it out but it still doesn't work for my own project. Also, I'm still a beginner in PHP.
So here is my problem. I wanted to try coding a simple edit form after I have finished coding the delete and view form.
This is my code
<?php
require("config.php");
$id = $_GET['id'];
echo "id: ".$id;
$sql = "SELECT * FROM contracts WHERE id= '$id'";
$result = $con->query($sql);
$row = $result->fetch_assoc()
?>
<form action="editform.php" method="GET">
ID:
<?php echo $id; ?><br>
Contract Title<br>
<input type="text" name="contract_title" value="<?php echo $row['contract_title']; ?>" /><br>
<input type="submit" name = "edit "value="Update" />
</form>
?php
if(isset($_POST['edit']) ){
$id = $_GET['id'];
$upd= "UPDATE `contracts` SET
`contract_title`='".$_POST['contract_title']."',
WHERE `id`='".$_POST['id']."";
if($do_upd = $con->query($upd))
{
echo "Update Success";
}
else
{
echo "Update Fail";
}
}
?>
This is the before the error.
This is the error I received.
In line 3, the id is not retrieved after I clicked the button update.
It didn't retrieved the values.
What mistakes did I do in the coding and how do I fix it? Thanks in advance.
Right below:
<form action="editform.php" method="GET">
Add:
<input type="hidden" name="id" value="<?php echo $id; ?>" />
Update:
Fixed other errors in your code:
<?php
require("config.php");
$id = $_GET['id'];
echo "id: ".$id;
$sql = "SELECT * FROM contracts WHERE id= '$id'";
$result = $con->query($sql);
$row = $result->fetch_assoc()
?>
<form action="editform.php" method="GET">
ID: <?php echo $id; ?><br>
Contract Title<br>
<input type="hidden" name="id" value="<?php echo $id; ?>" />
<input type="text" name="contract_title" value="<?php echo $row['contract_title']; ?>" /><br>
<input type="submit" name="edit" value="Update" />
</form>
<?php
if(isset($_GET['edit']) ){
// needs escaping!~~~
$upd= "UPDATE `contracts` SET `contract_title` = '".$_GET['contract_title']."' WHERE `id` = '".$id;
if($do_upd = $con->query($upd)) {
echo "Update Success";
} else {
echo "Update Fail";
}
}
Please consider escaping your database input to prevent SQL injection
<?php
require("config.php");
$id = $_GET['id'];
echo "id: ".$id;
$sql = "SELECT * FROM contracts WHERE id= '$id'";
$result = $con->query($sql);
$row = $result->fetch_assoc()
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$contract_title = $row['contract_title'];
}
} else {
echo "0 results";
}
if(isset($_POST['edit']) ){
$upd = "UPDATE contracts SET contract_title='$contract_title' WHERE id='$id'";
if($do_upd = $con->query($upd))
{
echo "Update Success";
}
else
{
echo "Update Fail";
}
}
?>
<form action="" method="POST">
ID:
<?php echo $id; ?><br>
Contract Title<br>
<input type="text" name="contract_title" value="<?php echo $row['contract_title']; ?>" /><br>
<input type="submit" name = "edit" value="Update" />
</form>

Display records from database

I'm trying to display some information stored in MySQL comments table to an input but I'm having issues with that. Input named enterComment inserts data to my DB and I want it to redirect back to showComment input.
HTML:
form action='takedata.php' method='POST'>
<input type='text' id='enterComment' name='enterComment' width='400px' placeholder='Write a comment...'>
<input type='submit' id='combuton' name='comButon' value='Comment!'>
<input type='text' id='showComment' name='showComment'>
</form>
PHP:
<?php include "mysql.php"; ?>
<?php
if (isset($_POST['comButon'])){
$enterComment = strip_tags($_POST['enterComment']);
if($enterComment){
$addComment = mysql_query("INSERT INTO comments(comment) VALUES('$enterComment')");
if($addComment==1)
//INSERT INTO showComment input;
}
}
?>
try this, and use mysqli instead of mysql
include "dbconnect.php";
if (isset($_POST['comButon'])){
$enterComment = strip_tags($_POST['enterComment']);
if($enterComment){
$addComment = mysqli_query($conn, "INSERT INTO comments(comment) VALUES('$enterComment')");
if($addComment) {
$sql = "select comment from comments order by id desc limit 1";
$result = mysqli_query($conn, $sql);
while($row = $result->fetch_assoc()) { ?>
<input type="text" value="<?php echo $row['comment']; ?>">
<?php }
}
}
}
your form
<form action='' method='POST'>
<input type='text' id='enterComment' name='enterComment' width='400px' placeholder='Write a comment...'>
<input type='submit' id='combuton' name='comButon' value='Comment!'>
<?php if(!isset($_POST['comButon'])) { ?>
<input type="text" value="">
<?php } ?>
</form>

Checkbox inside a php foreach loop to delete whatever is checked

echo "<form name='input' action='admin_selecteren_voor_verwijderen.php' method='post'>";
$sql_bestelling= "SELECT * FROM producten";
foreach($dbh->query($sql_bestelling) as $row)
{
$product_id=$row['product_id'];
$product_naam=$row['product_naam'];
$prijs=$row['prijs'];
$foto=$row['foto'];
echo "
<br>
<img src='$foto' height='70' width='50' border='0'>
<b>$product_naam</b> <input type='checkbox' name='$product_naam' value='$product_naam'></br>
</br></br></br>";
//if (isset($_POST['submit'])){
// $sql = "DELETE FROM `producten` WHERE product_naam='$product_naam'";
// $query = $dbh->prepare( $sql );
// $result = $query->execute();
//}
}
if(!empty($_POST['checkbox'])) {
foreach($_POST['checkbox'] as $check) {
echo "check: ", $check;
}
}
echo "
<input type='submit' value='Delete'>
</form>";
?>
I want to have a list of product in my webshop administrator page. Every product has a checkbox. I want to be able to delete all of the product of the checkboxes are checked. But I don't know how.. The above is what I have so far. The added picture is how it looks on the page. But the page is a list of product selected from the database with a foreach loop as can be seen in the code. The checkbox also is in the loop. I don't know how to assign every product which is check to a variable and then delete them from the database. Can anyone help me?
Name all the checkboxes a same, like delete[] , and and put the name of product in the value of each checkbox.
Example :
<form action="..." method='post' >
user1<input type="checkbox" name="delete[]"
value="<?php echo $product_naam ?>" /><br />
user2<input type="checkbox" name="delete[]"
value="<?php echo $product_naam ?>" /><br />
user3<input type="checkbox" name="delete[]"
value="<?php echo $product_naam ?>" /><br />
user4<input type="checkbox" name="delete[]"
value="<?php echo $product_naam ?>" /><br />
<input type='submit' value='delete' />
</form>
Delete query :
<?php
if(isset($_POST['delete'])){
$ids = $_POST['delete'];
$sql = "DELETE FROM `producten` WHERE product_naam
IN('".implode("','", $ids)."')";
//execute query
}
?>
You're looking for $_POST['checkbox'] but that's not what you have in your form. Name the checkboxes all checkbox[] and use $product_naam as the value.
<input type='checkbox' name='checkbox[]' value='$product_naam'>
Now you can loop over it and delete with your foreach loop.
you should not name the checkbox to the product name, just call it delete or something. Then you can use the foreach method to delete them from the database, like this:
<?php
echo "<form name='input' action='admin_selecteren_voor_verwijderen.php' method='post'>";
$sql_bestelling= "SELECT * FROM producten";
foreach($dbh->query($sql_bestelling) as $row)
{
$product_id=$row['product_id'];
$product_naam=$row['product_naam'];
$prijs=$row['prijs'];
$foto=$row['foto'];
echo "
<br>
<img src='$foto' height='70' width='50' border='0'>
<b>$product_naam</b> <input type='checkbox' name=delete' value='$product_naam'></br>
</br></br></br>";
//if (isset($_POST['submit'])){
// $sql = "DELETE FROM `producten` WHERE product_naam='$product_naam'";
// $query = $dbh->prepare( $sql );
// $result = $query->execute();
//}
}
if(isset($_POST['delete'])) {
foreach($_POST['delete'] as $delete){ {
$sql = "DELETE FROM producten WHERE product_naam= $delete";
$query = $dbh->prepare( $sql );
$result = $query->execute();
}
}
echo "
<input type='submit' value='Delete'>
</form>";
?>
Also, it seems to me you're mistaking "value" with "name", read up on it. And it would be good to read something about PDO, mysql isn't safe. http://www.php.net/manual/en/book.pdo.php

PHP Form update will not update

I have written an Edit part of my Form but somehow it will not Update the edited Fields. The Code does not give any Errors but it will not update?!
If possible could somebody take a look please?
<?php
include "connect.php";//database connection
if (isset($_GET["id"])) {
$id = intval($_GET["id"]);
if (isset($_POST["edited"]))
{
$update = "UPDATE traumprojekt SET";
$update .= sprintf("quantityProduct='%s', " , mysql_real_escape_string($_POST["quantityProduct"]));
$update .= sprintf("titleProduct='%s', " , mysql_real_escape_string($_POST["titleProduct"]));
$update .= sprintf("informationProduct='%s'", mysql_real_escape_string($_POST["informationProduct"]));
$update .= "WHERE id = '$id'";
mysql_query($update);
}
$sql = "SELECT * FROM `traumprojekt` WHERE id=$id";
$res = mysql_query($sql) or die (mysql_error());
if (mysql_num_rows($res) == 1)
{
$row = mysql_fetch_assoc($res);
?>
<form method="post" action="edit_form.php?id=<?php echo $row["id"] ?>">
ID: <?php echo $row["id"] ?><br />
Quantity: <input type="text" name="quantityProduct" value="<?php echo $row["quantityProduct"] ?>"><br />
Product Title: <input type="text" name="titleProduct" value="<?php echo $row["titleProduct"] ?>"><br />
Product Information: <input type="text" name="informationProduct" value="<?php echo $row["informationProduct"] ?>"><br />
<input type="submit" name="submit" value="Update"><br />
<input type="hidden" name="edited" value="1">
</form>
<?php
}
}
?>

invalid argument supplied for foreach() php

i have index.php and index2.php
here is index.php:
when i submit this value, index2.php will show. Here is index2.php:
I am not understanding, why this error msg is showing.
Anyways, submitting these selected values(index2.php) it will say a disease name from the database. clicking on submit, this text is showing(below):
I am pesting my code here:
index.php
<?php
$con = mysql_connect("localhost","root","");
mysql_select_db("symptomchecker",$con) or die(mysql_error());
$q = "select dtid,diseasetype from disease_type";
$result = mysql_query($q,$con) or die(mysql_error());
$numRB = mysql_num_rows($result);
echo "<html><head><title>Disease Type</title></head><body><h1>Disease List</h1></br><form action='index2.php' method='post'>";
$i=0;
while($row = mysql_fetch_array($result)){
echo $row['diseasetype'];
echo "<input type='radio' name='diseasetype' id='pet1' value='$row[dtid]' />" ;
echo "<br />";
$i++;
}
echo " <input type='submit' value='submit' name='submit'></form></body></html>";
?>
index2.php:
$diseasetypeid = "$_POST[diseasetype]";
$j=0;$i=0;
$query = "select did from disty_dis_rel where dtid = ".$diseasetypeid."";
$result1 = mysql_query($query,$con) or die(mysql_error());
echo "<html><head><title>symptom</title></head><body><h1>symptom List</h1></br><form action='' method='post'>";
while($row = mysql_fetch_array($result1))
{
$z=$row['did'];
$query2 = "select sid,symptomname from symptom where sid = ".$z."";
$result2 = mysql_query($query2,$con) or die(mysql_error());
while($row2 = mysql_fetch_array($result2))
{
echo $row2['symptomname'];
echo "<input type='checkbox' name='pets[$i]' id='pet1' value='$row[sid]' />" ;
echo "<br />";
$i++;
}
$j++;
}
echo " <input type='submit' value='submit' name='submit'></form></body></html>";
if(isset($_REQUEST[submit]))
{
**foreach ($_REQUEST[pets] as $key=>$values)**
{
$yy.=$values."-";
}
$yy=rtrim($yy,"-");
$xx = "select did,sids from dis_sym_rel where sids=".$yy."";
$result3 = mysql_query($xx,$con) or die(mysql_error());
while($row = mysql_fetch_array($result3)){
$p=$row[did];
$xxx = "select did,diseasename from disease where did=".$p."";
$result4 = mysql_query($xxx,$con) or die(mysql_error());
while($row4 = mysql_fetch_array($result4))
{
echo $row4[diseasename];
}
}
}
?>
the line between ###doublestar### is the error line(49).
Just within index2.php :
$diseasetypeid = "$_POST[diseasetype]";
should be
$diseasetypeid = $_POST["diseasetype"];
and
if(isset($_REQUEST[submit]))
should really be this :
if($_SERVER['REQUEST_METHOD'] == 'POST')
this checks the REQUEST_METHOD rather than a form value. And
foreach ($_REQUEST[pets] as $key=>$values)
should be
foreach ($_REQUEST['pets'] as $key=>$values)
and I suggest you read about sql injection
This code:
foreach ($_REQUEST[pets] as $key=>$values)
Should be changed to:
foreach ($_REQUEST['pets'] as $key=>$values)
You should quote (put apostrophe or double-quotes, before and after your array key if it's string).
Then, you must have a form something to the following (i can't find it in your index.php file).
<input type="text" name="pets[]" />
<input type="text" name="pets[]" />
<input type="text" name="pets[]" />
<input type="text" name="pets[]" />
<input type="text" name="pets[]" />
The array key 'pets' is the name of your input. [] indicates array. In foreach ($something as ...), $something must be an array (or object).

Categories