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
Related
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.
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']))
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Hey I have a database that contains information about different countries. I also have a html page where you can submit information about countries. Can someone help me to write a code that says that the information has been stored in the database instead of it just redirecting to a blank page?
Here is my html page where you submit information:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Sett inn land</title>
</head>
<body>
<form action="geoinn.php" method="get">
Land: <input type="text" name="navn"> <br>
Hovedstad: <input type="text" name="hovedstad"> <br>
Areal: <input type="text" name="areal"> <br>
Folketall: <input type="text" name="folketall"> <br>
<input type="submit" value="Legg inn informasjon">
</form>
</body>
</html>
And here is the page that you are redirected to when you click submit on the other page. This is the page where I want to have a code saying either that "The information has been stored in our database" or that it has not:
<?php
$tjener = "localhost";
$brukernavn = "root";
$passord ="";
$database ="Geografi";
$kobling = mysqli_connect($tjener,$brukernavn,$passord,$database);
$kobling->set_charset("utf8");
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Geografi</title>
</head>
<body>
<?php
$land = $_GET["navn"];
$hovedstad = $_GET["hovedstad"];
$areal = $_GET["areal"];
$folketall = $_GET["folketall"];
$sql ="INSERT INTO land (navn,hovedstad, areal, folketall) VALUES('$land','$hovedstad','$areal', '$folketall')";
mysqli_query($kobling, $sql);
mysqli_close($kobling);
?>
</body>
</html>
Add some output and you will get some output. The blank page you get is the page that does the updates. You add the basic HTML page tags but do not output anything inside the <body>
$sql ="INSERT INTO land
(navn,hovedstad, areal, folketall)
VALUES('$land','$hovedstad','$areal', '$folketall')";
$result = mysqli_query($kobling, $sql);
if ( $result === FALSE ) {
echo '<p>FAILED MESSAGE</p>';
} else {
echo '<p>SUCCESS MESSAGE</p>';
}
echo "<p>Land = $land</p>";
echo "<p>hovedstad = $hovedstad</p>";
// and so on
mysqli_close($kobling);
As Jay says,
Your script is at risk for SQL Injection Attacks. Learn about prepared statements for MySQLi.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
This is my page
I would make possible that if somebody writes in that imput form a word, PHP redirects him to a page. Is it possible? Can you help me? Thank you alot.
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/index.css">
<title>Magico mondo di Rob</title>
</head>
<body background="img/bg.png">
<div id="paroladiv">
<form>
<center>
<p1> Benvenuto! </p1>
</center>
<center>
<p2 id="parolatext">Inserisci la tua parola magica.</p2>
</center>
<p> </p>
<center>
<form action="" method="POST">
<input id="parola" type="text" name="parola"
placeholder="Parola va qua."><br>
</form>
</center>
</body>
</html>
<?php
if ($_POST['parola'] == google) {
header("Location: http://google.com");
}
?>
You have two things wrong .. Your HEADER needs to be located at the top of the file to prevent "Headers already sent" error. -- Plus it's always a good idea to throw a DIE in afterward for good measure.
Second, you need to put your POST query in quotes as it is a string you are looking for.
Plus as Fred mentioned in comments, you have a stray FORM tag
<?php
if ( !empty($_POST['parola']) && $_POST['parola'] == 'google') {
header("Location: http://google.com");
die();
}
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/index.css">
<title>Magico mondo di Rob</title>
</head>
<body background="img/bg.png">
<div id="paroladiv">
<center>
<p1> Benvenuto! </p1>
</center>
<center>
<p2 id="parolatext">Inserisci la tua parola magica.</p2>
</center>
<p> </p>
<center>
<form action="" method="POST">
<input id="parola" type="text" name="parola" placeholder="Parola va qua."><br>
</form>
</center>
</body>
</html>
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