I have a PHP generated HTML table that loads data from a MySQL database, in this table there is a button used to edit some information... When the button is pressed it sends the user to another page with a text field and a button.
The user can insert the new text and by pressing the button the new information should be stored in the DB, changing the old information with the new one…
But when I click the button to submit the new information, the following error appears:
Notice: Undefined index: ident in /...patch.../upload2.php on line 11
What I'm doing wrong? (I'm new to PHP)
Here is my code:
Resposta.php
<?php
ini_set('default_charset','UTF-8');
$con=mysqli_connect(“*******”,”*******”,”*******”,”*******”);
mysqli_set_charset($con,"utf8");
if (mysqli_connect_errno($con))
{
echo '{"query_result":"ERROR"}';
}
$emp_id = $_GET['id'];
$result = mysqli_query($con,"SELECT * FROM prefeitura WHERE id = $emp_id") ;
while($row = mysqli_fetch_array($result))
{
if($row['ID']) {
echo '<p><b>ID: </b>'. $row['ID'] .'';
}
if($row['nome']) {
echo '<p><b>SOLICITANTE: </b>'. $row['nome'] .'';
}
if($row['rua']) {
echo '<p><b>RUA: </b>'. $row['rua'] .'';
}
if($row['bairro']) {
echo '<p><b>BAIRRO: </b>'. $row['bairro'] .'</p>';
}
if($row['problema']) {
echo '<p><b>PROBLEMA: </b>'. $row['problema'] .'';
}
echo '<br>';
if($row['solucionado']) {
echo '<br><p><b>SITUAÇÃO: </b>'. $row['solucionado'] .'';
}
}
echo '<br><br><form enctype="multipart/form-data" action="upload2.php" method="post">';
echo '<br><input type="text" class="input-text text-area" name="resposta" id="resposta" placeholder="Escreva a resposta" required/>';
echo '<input type="submit" class="input-btn" ident="' .$row['ID']. '" value="Enviar Resposta" />';
mysqli_close($con);
?>
upload2.php
<?php
ini_set('display_errors', true); error_reporting(E_ALL);
ini_set('default_charset','UTF-8');
$con=mysqli_connect(“*******”,”*******”,”*******”,”*******”);
mysqli_set_charset($con,"utf8");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$emp_id = $_POST['ident'];
$resposta = nl2br(htmlentities($_POST['resposta'], ENT_QUOTES, 'UTF-8'));
$result = mysqli_query($con,"UPDATE prefeitura SET solucionado = '$resposta' WHERE ID = '$emp_id';");
header('Location: mensagem_enviada.html');
?>
I managed to solve the problem: the ID value was not being sent when pressing the button...
here's the piece of code that worked:
$mudaid = $_GET['id'];
echo <<<HTML
<form enctype="multipart/form-data" action="upload2.php" method="post">
<br><textarea rows="10" cols="70" name="resposta" id="resposta"></textarea>
<br><button type="submit" name="ident" value="$mudaid">Enviar resposta</button>
HTML;
echo '</center>';
First of all, the problem:
Notice: Undefined index: ident in /...patch.../upload2.php on line 11
this is telling you there is no 'ident' in $_POST variable.
Check out if 'ident' is set when you send the form.
This is yours..
'<input type="submit" class="input-btn" ident="' .$row['ID']. '" value="Enviar Resposta" />';
You should do something like this->
(edited)
'';
You should adapt this in order to make it work. I might be missing a ' . ;
Then, another thing im seeing, you should not do this, you might be vulnerable to SQL INJECTION.
$result = mysqli_query($con,"UPDATE prefeitura SET solucionado = '$resposta' WHERE ID = '$emp_id';");
You should use prepare statements and PDO.
http://php.net/manual/en/pdo.prepared-statements.php
Related
I have two tables in db, input table nd output table. input table got two columns $ID (autoincreament), $question. table 2 got two columns $Q_ID, Answer.
i made a php page (1) where i printed out the table 1 content (QUESTION) and i added a text area to get answers from visitors, along with it i also added a echo "hidden" field for ID from table one.
Now this php page 1 redirects to php page 2, where i POST the text area input into the answer column in table 2. But unfortunately, the ID is not able to get posted in q_id column of table 2...
I tried a lot but no hope..
My PHP script #1 as follows::
<div class="name">
<?php
$sql = "SELECT * FROM input";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$index = 0;
while($row = $result->fetch_assoc()) {
$index++; // stuff inside foreach goes here
?>
<div id="q">
<B><big><font color= #ba4a00> Q:</font></big> <?php echo $row["question"]; ?>
<?php
echo '<button class="add" id="add_'.$index.'"><B>Add Answer</B></button>';
echo '<form style="display:none;" name="answer_'.$index.'" method="post" action="output.php">';
echo '<input type="hidden" name="questionid" value="<?php echo $row[id]?>"/>';
echo '<textarea type="text" class="addtext" name="addtext" required id="addtext_'.$index.'" placeholder="Please type your answer here.." ></textarea>';
echo '<button onClick="addsubmit('.$index.');" type="submit" id="addsubmit_'.$index.'" class="addsubmit"><B>Submit</B></button>';
echo '</form>';
?>
</div>
<?php
}
} else {
echo "0 results";
}
$conn->close();
?>
</div>
My PHP script #2 as follows::
<?php include('1.php'); ?>
<?php
$servername = "localhost";
$dbusername = "root";
$dbpassword = "*******";
$dbname = "the_database";
$addtext = $_POST['addtext'];
$questionid = $_POST['questionid'];
$date = date_default_timezone_set('Asia/Kolkata');
$date = date('M-d,Y H:i:s');
$conn = new mysqli ($servername, $dbusername, $dbpassword, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO output (question_id, answer, date)
VALUES ('$questionid', '$addtext', '$date')";
if ($conn->query($sql) === TRUE) {
echo '<script language="javascript">';
echo 'alert("Your Answer has been Succesfully posted")';
echo '</script>';
echo '';
}
else {
echo "ERROR" . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
i want the respective question id to be passed to table 2 along with its answer.
Any help is greatly appreciated..
Give single-quote in $row['id']. Change this <?php echo $row[id] >? to <?php echo $row['id'] >?
echo '<input type="hidden" name="questionid" value="'. $row['id'].'"/>';
Already you are echoing the form using the ECHO Attribute in the PHP but after that also you have echoed it and that is wrong.
You have to make the code like this.
echo '<input type="hidden" name="questionid" value="'. $row['id'].'"/>';
Try this and share the thought about the code..
try enclosing id in double quotes in php 1:
echo '<input type="hidden" name="questionid" value="'.$row['id'].'"/>';
yip... looking a this a bit more closely you've got a bit of a problem with mixing single and double quotes in PHP #1.
for example, on this line the echo command ends after the second single quote after add_
echo '<button class="add" id="add_'.$index.'"><B>Add Answer</B></button>';
You need to use either single or double quotes consistently.
I have a table in data base with columns like ID,question,date. and an php page (1.php)which outputs question and date from db table and takes answer in textarea. when clicked submit, it redirects to another php page (2.php) where i m supposed to store the typed answer into another table in db.
here, my problem is how can i add question_ID to the second table in db along with answer.
1.PHP script as follows::
<div class="name">
<?php
$sql = "SELECT * FROM input";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$index = 0;
while($row = $result->fetch_assoc()) {
$index++;
?>
<div id="q">
<?php echo $row["question"]; ?> </B>
<?php
echo '<button class="add" id="add_'.$index.'"><B>Add Answer</B></button>';
echo '<form style="display:none;" name="answer_'.$index.'" method="post" action="output.php">'; // I dont think openning form from row to row would be nice!
echo '<textarea type="text" class="addtext" name="addtext" required id="addtext_'.$index.'" placeholder="Please type your answer here.." ></textarea>';
echo '<button onClick="addsubmit('.$index.');" type="submit" id="addsubmit_'.$index.'" class="addsubmit"><B>Submit</B></button>';
echo '</form>';
?>
<small><p><?php echo $row["date"]; ?></p></small>
2.PHP script as follows::
<?php include('1.php'); ?>
<?php
$servername = "localhost";
$dbusername = "root";
$dbpassword = "******";
$dbname = "the_database";
$addtext = $_POST['addtext'];
$date = date_default_timezone_set('Asia/Kolkata');
$date = date('d/m/Y H:i:s');
$conn = new mysqli ($servername, $dbusername, $dbpassword, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO output (answer, date)
VALUES ('$addtext', '$date')";
if ($conn->query($sql) === TRUE) {
echo '<script language="javascript">';
echo 'alert("Your Answer has been Succesfully posted")';
echo '</script>';
echo '';
}
else {
echo "ERROR" . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
Answer and date saved in db table successfully but got stuck in getting question_ID...
Any help is greatly Appreciated.
Add the hidden field:
on 1.php
echo '<form style="display:none;" name="answer_'.$index.'" method="post" action="output.php">'; // I dont think openning form from row to row would be nice!
echo '<input type="hidden" name="questionid" value="<?php echo $row[ID]?>"/>'
echo '<textarea type="text" class="addtext" name="addtext" required id="addtext_'.$index.'" placeholder="Please type your answer here.." ></textarea>';
echo '<button onClick="addsubmit('.$index.');" type="submit" id="addsubmit_'.$index.'" class="addsubmit"><B>Submit</B></button>';
echo '</form>';
on page 2.php
$qid = $_POST['questionid']; //get the question id and insert in the table
This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 7 years ago.
I've got a database with a Users table which I'm trying to update.
Currently I have customers.php, which displays form fields with the user information so it can be updated.
This form points to edit_customer_processor.php , which takes the new values, puts them into a MYSQL query... and then despite the query working correctly when I query the DB via the PHPMyAdmin command line, the record doesn't update.
customers.php
<?php
session_start();
if(!$_SESSION["logged_in"]){
header("location:home.php");
die;
}
?>
<?php include 'header.html'; ?>
<div id='maincontent'>
<?php
if (isset($_GET["id"])){
$customer_id = $_GET["id"];
require_once('config.php');
$customer_query = "SELECT * FROM customer WHERE customer_id = $customer_id";
$customer_results = mysql_query($customer_query, $conn);
if (!$customer_results) {
die ("Error selecting car data: " .mysql_error());
}
else {
while ($row = mysql_fetch_array($customer_results)) {
echo "<h3>Edit Customer</h3>";
echo "<FORM method='post' action='edit_customer_processor.php'>";
echo '<p> Name: <input type="text" name="name" size = "40" value=' . $row[name] . '></p>';
echo '<p> Address: <input type="text" name ="address" size="40" value=' . $row[address] . '></p>';
echo '<p> Email: <input type="text" name="email" value=' . $row[email] . '></p>';
echo '<p> Phone: <input type ="text" name="phone" size="20" value=' . $row[phone] . '></p>';
echo '<input type ="hidden" name="customer_id" value="' . $row[customer_id] . '">';
echo '<input type ="hidden" name="formtype" value="edit_customer">';
echo '<input type="submit" name="submit" value= "Update">';
echo '</form>';
}
}
} else {
// If there isn't an ID, display the New Customer form and all customers below, with links
// to their edit pages.
echo "<h3>Enter new customer information and submit.</h3>";
echo "<FORM method='post' action='new_customer_processor.php'>";
echo '<p> Name: <input type="text" name="name" size = "40"></p>';
echo '<p> Address: <input type="text" name ="address" size="40"></p>';
echo '<p> Email: <input type="text" name="email"></p>';
echo '<p> Phone: <input type ="text" name="phone" size="20"></p>';
echo '<input type ="hidden" name="formtype" value="new_customer">';
echo '<input type="submit" name="submit" value= "Submit">';
echo '<input type ="reset" name="reset" value ="Reset">';
echo '</form>';
require_once('config.php');
echo "<h3>Current Customers</h3>";
$query = "SELECT * FROM customer";
$results = mysql_query($query, $conn);
if (!$results) {
die ("Error selecting customer data: " .mysql_error());
}
else {
// In the absence of an ID, all customers will be displayed down
// the bottom of the page
while ($row = mysql_fetch_array($results)) {
echo "<a href=customers.php?id=";
echo $row[customer_id];
echo "><p> $row[name] </p></a>";
echo "<p> $row[address] </p>";
echo "<p> $row[phone] </p>";
echo "<p> $row[email] </p>";
}
}
}
?>
Back to Customers Page
</div>
<?php include 'footer.html' ?>
edit_customer_processor.php
<?php include 'header.html' ?>
<div id="maincontent">
<?php
// Pulling in hidden customer ID from post value
$mysqli = new mysqli( 'localhost', 'root', 'root', 'w_c_a' );
// Check our connection
if ( $mysqli->connect_error ) {
die( 'Connect Error: ' . $mysqli->connect_errno . ': ' . $mysqli->connect_error );
}
// Insert our data
$sql = mysql_query("UPDATE customer
SET name = '".mysql_real_escape_string($_POST['name'])."',
address = '".mysql_real_escape_string($_POST['address'])."',
phone = '".mysql_real_escape_string($_POST['phone'])."',
email = '".mysql_real_escape_string($_POST['email'])."'
WHERE customer_id='".mysql_real_escape_string($_POST['customer_id'])."'");
$update = $mysqli->query($sql);
echo "Customer updated: ";
echo "<a href=customers.php?id=" . $_POST['customer_id'] . ">";
echo "Back to Edit Customer</a>";
?>
</div>
<?php include 'footer.html' ?>
And when I echo the MYSQL query, I get:
UPDATE customer SET name = 'Kellyassdsa', address = 'ads', phone = '0260123123', email = 'asdasd' WHERE customer_id='1'
Which works when I put it in PHPMyAdmin.
I know it'll be some boneheaded little mistake, but I've been trying to get this work for ages now. Any ideas?
Maybe your program just can't connect to your MySQL database.
$customer_results = mysql_query($customer_query, $conn);
I can't see where you gave a value to the var $conn.
If the problem is connection problem then we might need your database info like the name of your table in PhpMyAdmin.
your problem is...
$sql = mysql_query(..);
$update = $mysqli->query($sql);
it should be
$sql = 'UPDATE ...';
$update = $mysqli->query($sql);
i think problem occurs due to line break. pleas make a query in single line without line break.
$sql = mysql_query("UPDATE customer SET name = '".mysql_real_escape_string($_POST['name'])."',address = '".mysql_real_escape_string($_POST['address'])."', phone = '".mysql_real_escape_string($_POST['phone'])."', email = '".mysql_real_escape_string($_POST['email'])."' WHERE customer_id='".mysql_real_escape_string($_POST['customer_id'])."'");
Hope this helps..
I'm new to php and MySQL (working through "Head First: PHP & MySQL"). I'm trying to grab a MySQL table column name from an array and store it in the "value" for an input field of a form. For some reason, I only end up with a blank page. When I simply type in some text into the 'value', the page turns out fine.
I've checked every line of code and have narrowed it down to the input value. The database connection works and I can echo the column name in the 'while' loop, but not in the 'value'.
Here's the code:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?> ">
<?php
$dbc= mysqli_connect('host', 'user', 'pass', 'elvis_store')
or die ('Error connecting to the database');
$query= "SELECT * FROM elvis_emails";
$result= mysqli_query($dbc, $query)
or die ('Error querying database');
while ($row= mysqli_fetch_array($result)){
echo '<input type="checkbox" value="'$row['Id']'" name="todelete[]" />';
echo $row['Id'];
echo ' ' . $row['first_name'];
echo ' ' . $row['last_name'];
echo ' ' . $row['email'];
echo '<br />';
}
mysqli_close($dbc);
?>
<input type="submit" name="submit" value="Remove" />
</form>
Thank you!
instead of,
echo '<input type="checkbox" value="'$row['Id']'" name="todelete[]" />';
try like this
echo '<input type="checkbox" value="'.$row["Id"].'" name="todelete[]" />';
What is really breaking the code is the missing of proper concatenation.
It's good practice to avoid printing HTML elements with PHP echo statement. Rather, the best approach in this case would be:
<?php while ($row = mysqli_fetch_array($result)){ ?>
<input type="checkbox" value="<?=$row['Id']?>" name="todelete[]" />
<?php
...
}
mysqli_close($dbc);
?>
If by anyways you do need to print a piece of code that is relying on HTML tags and PHP variables you can make use of HEREDOC instead of using echo to print multiple lines of code.
Also, make sure that you are naming the keys to the row array the
same as your query return values.
I'm trying to make a small survey that populates the selections for the dropdown menu from a list of names from a database. The survey does this properly. I want to submit the quote the user submits with this name into a quote database. The quote text they enter into the field goes in properly, however, the name selected from the menu does not get passed in. Instead I get a blank name field.
I understand some of my code is out of context, but the name is the only thing that does not get passed in properly.
On form submit, I include the php file that submits this data to the database:
<form action="<?php $name = $_POST['name']; include "formsubmit.php";?>" method="post">
<label> <br />What did they say?: <br />
<textarea name="quotetext" rows="10" cols="26"></textarea></label>
<input type="submit" value="Submit!" />
</form>
The variable $name comes from this (which populates my dropdown menu):
echo "<select name='name'>";
while ($temp = mysql_fetch_assoc($query)) {
echo "<option>".htmlspecialchars($temp['name'])."</option>";
}
echo "</select>";
And here is my formsubmit.php:
<?php:
mysql_select_db('quotes');
if (isset($_POST['quotetext'])) {
$quotetext = $_POST['quotetext'];
$ident = 'yankees';
$sql = "INSERT INTO quote SET
quotetext='$quotetext',
nametext='$name',
ident='$ident',
quotedate=CURDATE()";
header("Location: quotes.php");
if (#mysql_query($sql)) {
} else {
echo '<p> Error adding quote: ' .
mysql_error() . '</p>';
}
}
?>
Your form action stuff looks weird, but regardless, I think the problem you're having has to do with not setting $name = $_POST['name'] like you're doing with $quotetext = $_POST['quotetext']. Do that before the sql statement and it should be good to go.
edit to try to help you further, I'll include what the overall structure of your code should be, and you should tweak it to fit your actual code (whatever you're leaving out, such as setting $query for your name options):
file 1:
<form action="formsubmit.php" method="post">
<label> <br />What did they say?: <br />
<textarea name="quotetext" rows="10" cols="26"></textarea></label>
<select name='name'>
<?php
while ($temp = mysql_fetch_assoc($query)) {
echo "<option>".htmlspecialchars($temp['name'])."</option>";
}
?>
</select>
<input type="submit" value="Submit!" />
</form>
formsubmit.php:
<?php
mysql_select_db('quotes');
if (isset($_POST['quotetext'])) {
$quotetext = $_POST['quotetext'];
$name = $_POST['name'];
$ident = 'yankees';
$sql = "INSERT INTO quote SET
quotetext='$quotetext',
nametext='$name',
ident='$ident',
quotedate=CURDATE()";
if (#mysql_query($sql)) {
header("Location: quotes.php");
} else {
echo '<p> Error adding quote: ' .
mysql_error() . '</p>';
}
}
?>
echo "<select name='name'>";
while ($temp = mysql_fetch_assoc($query)) {
$nyme = htmlspecialchars($temp['name']);
echo "<option value='$nyme'>$nyme</option>";
}
echo "</select>";-
This way you will receive the value of the name in $_POST array
and you have to get that value out of $_POST array as well you need to change the
code add the following line to get the name in your script.
$name = $_POST['name'];
you need to change the form action tag
<form action='formsubmit.php' .....>
and in that file after successful insertion you can redirect the user to whereever.php.
so it was fun explaining you every thing bit by bit change this now in your code as well.
if (#mysql_query($sql)) {
header("Location: quotes.php");
} else {
echo '<p> Error adding quote: ' .
mysql_error() . '</p>';
}