Single submit button should perform multiple actions in php - php

I have two php pages.
dataentryform.php
report.php
In dataentryform.php through form tag I'm taking 19 user inputs with fcode being a primary key. Upon submit I want two things to happen.
data stored into the database
the data entered in the form should get displayed on the report.php
file.
My Problem:
Able to store values into the database and also retrieve it.
But, the values printed on the report.php file will always
correspond to the first row in the table
How do I fix this?
dataentryform.php
<form method="post" action="includes/dbinsert.php">
<table width="650" border="1" class="table1">
<tbody>
<tr>
<td class="label">Farmer's Code</td>
<td width="350" colspan="2"><input type="text" name="fcode"
class="text" autocomplete="off" required></td>
</tr>
<tr>
<td class="label">Farmer Name</td>
<td width="350" colspan="2"><input type="text" name="fname"
class="text" autocomplete="off" required></td>
</tr>
<tr>
<td class="label">Village/ Town</td>
<td colspan="2"><input type="text" name="village" class="text"
autocomplete="off" required></td>
</tr>
<tr>
<td class="label">Survey Number</td>
<td width="350" colspan="2"><input type="text" name="surnum"
class="text" autocomplete="off" required></td>
</tr>
<tr>
<td class="label">Plot Number</td>
<td width="350" colspan="2"><input type="text" name="plotnum"
class="text" autocomplete="off" required></td>
</tr>
...
...
...
...
...
...
...
...
...
...
...
...
<tr>
<td class="label">Potash (Tons/Acre)</td>
<td width="350" colspan="2"><input type="number" name="potash"
class="range" min="0" step="0.001" autocomplete="off" required></td>
</tr>
</tbody>
</table>
</div>
<button type="submit" name="submit" class="submit">Submit</button>
</div>
</form>
dbinsert.php
<?php
include 'dbconnect.php';
$fcode = $_POST['fcode'];
$fname = $_POST['fname'];
$village = $_POST['village'];
$surnum = $_POST['surnum'];
$plotnum = $_POST['plotnum'];
$acre = $_POST['acre'];
$gunta = $_POST['gunta'];
$soiltype = $_POST['soiltype'];
$wtrsrc = $_POST['wtrsrc'];
$factory = $_POST['factory'];
$labnum = $_POST['labnum'];
$nextcrop = $_POST['nextcrop'];
$coldate = $_POST['coldate'];
$gendate = $_POST['gendate'];
$season = $_POST['season'];
$taryield = $_POST['taryield'];
$nitro = $_POST['nitro'];
$phos = $_POST['phos'];
$potash = $_POST['potash'];
$sql = "INSERT INTO forminfo (fcode,fname,village,surnum,plotnum,acre,gunta,soiltype,wtrsrc,factory,labnum,
nextcrop,coldate,gendate,season,taryield,nitro,phos,potash)
VALUES ('$fcode','$fname','$village','$surnum','$plotnum','$acre','$gunta','$soiltype','$wtrsrc','$factory','$labnum',
'$nextcrop','$coldate','$gendate','$season','$taryield','$nitro','$phos','$potash');";
mysqli_query($conn, $sql);
header('location:../report.php');
?>
dbinsert.php is inserting values into the database. Then redirecting to report.php. Here I'm including dbextract.php. However, values displayed in report.php are incorrect.
Tried with this also but no luck.
dbextract.php
<?php
include 'dbconnect.php';
$fcode = $_POST['fcode'];
$sql = "SELECT * FROM forminfo WHERE fcode=['$fcode']";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($result);
?>
Error: Unidentified variable $fcode.

You could use session variables by adding
session_start();
$_SESSION['data'] = $_POST;
at the beginning of dbinsert.php and then using $fcode = $_SESSION['data']['fcode']; on dbextract.php

To claryfy Twista's answer - The Minimal, Complete, and Verifiable example would probably look like the example below.
NOTE:
I pretty much excluded everything that was causing any sort of errors, i also omitted the actual database insertion, since it doesn't really relate to the problem.
Judging by provided code the was actually one big problem:
Undefined indexes. Judging by provided code dbinsert.php has a lot of them. $acre = $_POST['acre'] and every $_POST[] after it should yeild Warning: Undefined index.
dbextract.php also has a very invalid SQL Query. It probably should have being SELECT * FROM forminfo WHERE fcode='$fcode'.
Actually it would be better if you used MySQLi Prepared Statements
The example below demonstrates working with $_SESSION.
dataentryform.php
<form method="post" action="dbinsert.php">
<table width="650" border="1" class="table1">
<tbody>
<tr>
<td class="label">Farmer's Code</td>
<td width="350" colspan="2"><input type="text" name="fcode" class="text" autocomplete="off" required></td>
</tr>
<tr>
<td class="label">Farmer Name</td>
<td width="350" colspan="2"><input type="text" name="fname" class="text" autocomplete="off" required></td>
</tr>
<tr>
<td class="label">Village/ Town</td>
<td colspan="2"><input type="text" name="village" class="text" autocomplete="off" required></td>
</tr>
<tr>
<td class="label">Survey Number</td>
<td width="350" colspan="2"><input type="text" name="surnum" class="text" autocomplete="off" required></td>
</tr>
<tr>
<td class="label">Plot Number</td>
<td width="350" colspan="2"><input type="text" name="plotnum" class="text" autocomplete="off" required></td>
</tr>
...
...
...
...
...
...
...
...
...
...
...
...
<tr>
<td class="label">Potash (Tons/Acre)</td>
<td width="350" colspan="2"><input type="number" name="potash" class="range" min="0" step="0.001" autocomplete="off" required></td>
</tr>
</tbody>
</table>
<button type="submit" name="submit" class="submit">Submit</button>
</form>
dbinsert.php
<?php
session_start();
// include 'dbconnect.php';
$fcode = $_POST['fcode'];
$fname = $_POST['fname'];
$village = $_POST['village'];
$surnum = $_POST['surnum'];
$plotnum = $_POST['plotnum'];
$_SESSION['fcode']=$fcode;
// $sql = "INSERT INTO forminfo (fcode,fname,village,surnum,plotnum,acre,gunta,soiltype,wtrsrc,factory,labnum,
// nextcrop,coldate,gendate,season,taryield,nitro,phos,potash)
// VALUES ('$fcode','$fname','$village','$surnum','$plotnum','$acre','$gunta','$soiltype','$wtrsrc','$factory','$labnum',
// '$nextcrop','$coldate','$gendate','$season','$taryield','$nitro','$phos','$potash');";
// mysqli_query($conn, $sql);
header('location: dbextract.php');
?>
dbextract.php
<?php
//session_start();
// include 'dbconnect.php';
$fcode = $_SESSION['fcode'];
//$fcode = $_GET['fcode'];
var_dump($fcode);
// $sql = "SELECT * FROM forminfo WHERE fcode=['$fcode']";
// $result = mysqli_query($conn, $sql);
// $row = mysqli_fetch_array($result);
?>
The same can be done without the use of $_SESSION. dataentryform.php stays the same.
dbinsert.php
<?php
//session_start();
// include 'dbconnect.php';
$fcode = $_POST['fcode'];
$fname = $_POST['fname'];
$village = $_POST['village'];
$surnum = $_POST['surnum'];
$plotnum = $_POST['plotnum'];
//$_SESSION['fcode']=$fcode;
// $sql = "INSERT INTO forminfo (fcode,fname,village,surnum,plotnum,acre,gunta,soiltype,wtrsrc,factory,labnum,
// nextcrop,coldate,gendate,season,taryield,nitro,phos,potash)
// VALUES ('$fcode','$fname','$village','$surnum','$plotnum','$acre','$gunta','$soiltype','$wtrsrc','$factory','$labnum',
// '$nextcrop','$coldate','$gendate','$season','$taryield','$nitro','$phos','$potash');";
// mysqli_query($conn, $sql);
header('location: dbextract.php/?fcode='.$fcode);
?>
dbextract.php
<?php
//session_start();
// include 'dbconnect.php';
//$fcode = $_SESSION['fcode'];
$fcode = $_GET['fcode'];
var_dump($fcode);
// $sql = "SELECT * FROM forminfo WHERE fcode=['$fcode']";
// $result = mysqli_query($conn, $sql);
// $row = mysqli_fetch_array($result);
?>
The above shows that it can be done without $_SESSION. The thing that happens in this exaple is that you pass your desired value as a param. Then the GET request happens, and then you use $_GET['fcode'] to get your value.
!!!IMPORTANT!!!
The exapmles above, are just that - examples. You will likely have to customise them a bit to suit your needs. For instance - you will have to actually un-comment parts, that are working with database and re-write them a bit.
Consearning sending data with a single POST request to several different .php files - On the server side as far as i am aware a single POST request sends data to a single file. Note it is possible to send several POST request using AJAX, but AJAX works on client side, so AJAX won't be of assistance if a redirect happens at server side.

Related

MySQL table page not loading correct data

I'm creating a table that uses PHP to pull from a MySQL database that I have. I think I've got everything where I want it to be, however the only problem I'm having is that the results seem to be (for lack of a better word) "behind". What I mean by that is that my first page index.php is where I'm accepting user edits to the database. Once they click Update it sends them to my results.php file that is supposed to actually perform the SQL UPDATE and then display the updated table.
It updates the table just fine according to XAMPP's database editor. However, when I said "behind" I mean that the page loads, updates but doesn't display the updated data until either the user refreshes the page or returns to the first page THEN comes back. I'm not sure what could be causing it, so I'm hoping someone here can help me. I feel like the reason is something as simple as I'm just running the code in the wrong order, but I don't know for sure. My code is below:
index.php
<html>
<body>
<?php
include('dbconnect.php');
$query = "SELECT * FROM vw_events";
$result = mysqli_query($conn, $query);
$count = mysqli_num_rows($result);
?>
<form name="form1" method="post" action="results.php">
<table width="auto" border="1" cellspacing="1" cellpadding="5">
<tr>
<td align="center"><strong>Event ID</strong></td>
<td align="center"><strong>Title</strong></td>
<td align="center"><strong>Topic</strong></td>
<td align="center"><strong>Description</strong></td>
<td align="center"><strong>Event Date</strong></td>
<td align="center"><strong>Speaker</strong></td>
<td align="center"><strong>Building</strong></td>
<td align="center"><strong>Room</strong></td>
</tr>
<?php
while($rows=mysqli_fetch_array($result)) {
?>
<tr>
<input name="event_id[]" type="hidden" id="event_id" value="<?php echo $rows['event_id']; ?>">
<td align="center">
<?php echo $rows['event_id'];?>
</td>
<td align="center">
<input name="title[]" type="text" id="title">
</td>
<td align="center">
<?php echo $rows['topic_name']; ?>
</td>
<td align="center">
<?php echo $rows['topic_description']; ?>
</td>
<td align="center">
<input name="date[]" type="date" id="date">
</td>
<td align="center">
<input title="Use reference tables below to enter speaker ID" name="speaker[]" type="text" id="speaker">
</td>
<td align="center">
<input title="Use reference tables below to enter building ID" name="building[]" type="text" id="building">
</td>
<td align="center">
<input title="Use reference tables below to enter Room ID" name="room[]" type="text" id="room">
</td>
</tr>
<?php
}
?>
<tr>
<td colspan="8" align="center"><input type="submit" name="Update" value="UPDATE"></td>
</tr>
</table>
</form>
</body>
</html>
results.php
<html>
<body>
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
error_reporting(E_ALL);
ini_set('display_errors',1);
require_once('dbconnect.php');
$query = "SELECT * FROM vw_events";
$result = mysqli_query($conn, $query);
$count = mysqli_num_rows($result);
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$id = $_POST['event_id'];
$title2 = $_POST['title'];
$date2 = $_POST['date'];
$speaker2 = $_POST['speaker'];
$building2 = $_POST['building'];
$room2 = $_POST['room'];
for($i=0;$i<$count;$i++) {
$sql="UPDATE events SET title='$title2[$i]', event_date='$date2[$i]', speaker='$speaker2[$i]', building='$building2[$i]', room='$room2[$i]' WHERE event_id='$id[$i]'";
$result1=mysqli_query($conn, $sql);
}
}
?>
<form name="form1" method="post" action="index.php">
<table width="auto" border="1" cellspacing="1" cellpadding="5">
<tr>
<td align="center"><strong>Event ID</strong></td>
<td align="center"><strong>Title</strong></td>
<td align="center"><strong>Topic</strong></td>
<td align="center"><strong>Description</strong></td>
<td align="center"><strong>Event Date</strong></td>
<td align="center"><strong>Speaker</strong></td>
<td align="center"><strong>Building</strong></td>
<td align="center"><strong>Room</strong></td>
</tr>
<?php
while($rows=mysqli_fetch_array($result)) {
?>
<tr>
<td align="center">
<?php echo $rows['event_id'];?>
</td>
<td align="center">
<?php echo $rows['title']; ?>
</td>
<td align="center">
<?php echo $rows['topic_name']; ?>
</td>
<td align="center">
<?php echo $rows['topic_description']; ?>
</td>
<td align="center">
<?php echo $rows['event_date']; ?>
</td>
<td align="center">
<?php echo $rows['speaker_name']; ?>
</td>
<td align="center">
<?php echo $rows['building_name']; ?>
</td>
<td align="center">
<?php echo $rows['room_name']; ?>
</td>
</tr>
<?php
}
?>
<tr>
<td colspan="8" align="center"><input type="submit" name="Return" value="Return"></td>
</tr>
</table>
</form>
</body>
</html>
Also if someone can give me some guidance as to how to run the htmlspecialchars function on my arrays within results.php I'd really appreciate it. I've already tried to create a for loop for literally each array but that didn't work. I've tried using ->
<?php
function htmlspecial_array(&$variable) {
foreach ($variable as &$value) {
if (!is_array($value)) { $value = htmlspecialchars($value); }
else { htmlspecial_array($value); }
}
}
but that also didn't work, and I've tried using the array_walk_recursive but to no avail. I want to try and do something like W3Schools' example here W3Schools Form Validation towards the bottom of the page where it says Validate Form Data With PHP and then gives an example.
The result you get from the UPDATE query is the number of affected rows in your database. To correctly display the updated data, you need to re-fetch from the database before you generate the HTML. You should rearrange your code in results.php like this:
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
error_reporting(E_ALL);
ini_set('display_errors',1);
require_once('dbconnect.php');
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$id = $_POST['event_id'];
$title2 = $_POST['title'];
$date2 = $_POST['date'];
$speaker2 = $_POST['speaker'];
$building2 = $_POST['building'];
$room2 = $_POST['room'];
$query = "SELECT * FROM vw_events";
$result = mysqli_query($conn, $query);
$count = mysqli_num_rows($result);
for($i=0;$i<$count;$i++) {
$sql="UPDATE events SET title='$title2[$i]', event_date='$date2[$i]', speaker='$speaker2[$i]', building='$building2[$i]', room='$room2[$i]' WHERE event_id='$id[$i]'";
$result1=mysqli_query($conn, $sql);
}
}
$query = "SELECT * FROM vw_events";
$result = mysqli_query($conn, $query);
Side note: If your data is sensitive, you may want to read about mysqli prepared statement so hackers cannot tamper with your queries.
Regarding your question about htmlspecialchars, see Stackoverflow "Execute htmlspecialchars on a multi level array".

PHP form can't be updated

I am currently making a system for a client database management. There are four tables in mySQL for this system, which are; admin, staff, client, and project. The project table has one foreign key from the client table, which is the clientid.
Now, I have made forms for all these tables so that the user can input the data into them. Weirdly, the only form that can be updated successfully is the staff one. Both the client and project forms cannot be updated at all. It returns as successful, but the data are not altered.
Below is the staff update code.
<?php
include 'database.php';
$staffid = $_GET['staffid'];
$sql = "SELECT * FROM staff WHERE staffid='$staffid'";
$result = mysqli_query($conn,$sql);
while ($row=mysqli_fetch_array($result)){
$staffname = $row['staffname'];
$staffemail = $row['staffemail'];
$staffphone = $row['staffphone'];
}
if(isset($_POST['submit'])){
$staffname = $_POST['staffname'];
$staffemail = $_POST['staffemail'];
$staffphone = $_POST['staffphone'];
$sql = "UPDATE staff SET
staffname='$staffname',staffemail='$staffemail',staffphone='$staffphone' WHERE staffid='$staffid'";
$result = mysqli_query($conn,$sql);
if($result){
echo "<table><td><tr><h4>Record has been updated successfully!<br></tr></td></h4></table>";
}
else {
echo "<h4>Record has <b>NOT</b> been updated successfully<br></h4>";
}
}
?>
<form action="" method="post">
<table class ="table1">
<tr>
<td>Staff Name:</td> <td><input type="text" name="staffname" size="50" value="<?php echo $staffname;?>"></td>
</tr>
<tr>
<td>Staff Email:</td> <td><input type="text" name="staffemail" size="50" value="<?php echo $staffemail;?>"></td>
</tr>
<tr>
<td>Staff Phone No:</td> <td><input type="text" name="staffphone" size="50" value="<?php echo $staffphone;?>"></td>
</tr>
<td><input type="submit" value="Update" name="submit"> <input type="button" value="View" name="view" onclick='location.href="viewstaff.php"'></td>
</table>
</form>
Okay now is the update code for the client table.
<?php
include 'database.php';
$clientid = $_GET['clientid'];
$sql = "SELECT * FROM client WHERE clientid='$clientid'";
$result = mysqli_query($conn,$sql) or die ("Error in query: $query. ".mysqli_error());
while ($row=mysqli_fetch_array($result)){
$clientid = $row['clientid'];
$clientname = $row['clientname'];
$clientno = $row['clientno'];
$clientemail = $row['clientemail'];
$clientadd = $row['clientadd'];
}
if(isset($_POST['submit'])){
$clientid = $row['clientid'];
$clientname = $row['clientname'];
$clientno = $row['clientno'];
$clientemail = $row['clientemail'];
$clientadd = $row['clientadd'];
$sql = "UPDATE client SET clientid='$clientid',clientname='$clientname',clientno='$clientno',clientemail='$clientemail',clientadd='$clientadd' WHERE clientid='$clientid'";
$result = mysqli_query($conn,$sql) or die ("Error in query: $query. ".mysqli_error());
if($result){
echo "<table><td><tr><h4>Record has been updated successfully!<br></tr></td></h4></table>";
}
else {
echo "<h4>Record has <b>NOT</b> been updated successfully<br></h4>";
}
}
?>
<form action="" method="post">
<table class ="table1">
<tr>
<td>Client ID:</td> <td><input type="text" name="clientid" size="50" value="<?php echo $clientid;?>"></td>
</tr>
<tr>
<td>Client Name:</td> <td><input type="text" name="clientname" size="50" value="<?php echo $clientname;?>"></td>
</tr>
<tr>
<td>Client Phone No.:</td> <td><input type="text" name="clientno" size="50" value="<?php echo $clientno;?>"></td>
</tr>
<tr>
<td>Client Email:</td> <td><input type="text" name="clientemail" size="50" value="<?php echo $clientemail;?>"></td>
</tr>
<tr>
<td>Client Address:</td> <td><input type="text" name="clientadd" size="50" value="<?php echo $clientadd;?>"></td>
</tr>
<td><input type="submit" value="Update" name="submit"> <input type="button" value="View" name="view" onclick='location.href="viewclient.php"'></td>
</table>
</form>
Maybe I'm stupid or what but I've been trying to figure out the problem for 3 hours and I'm this close to crying lol. Been reading all the threads here about updating form but still, no answer. Hope that anyone here could help me. Thank you.
The code you use for the client table update uses this code:
if(isset($_POST['submit'])){
$clientid = $row['clientid']; // $row should be $_POST
$clientname = $row['clientname']; // $row should be $_POST
$clientno = $row['clientno']; // $row should be $_POST
$clientemail = $row['clientemail']; // $row should be $_POST
$clientadd = $row['clientadd']; // $row should be $_POST
But those $rows should be $_POST, else the updated data will be the same as the previous data (since $row is the result from the query SELECT * FROM client WHERE clientid='$clientid'). You do it correctly in the staff table update code:
if(isset($_POST['submit'])){
$staffname = $_POST['staffname'];
$staffemail = $_POST['staffemail'];
$staffphone = $_POST['staffphone'];
Please note that your your script is at risk of SQL Injection Attack. Have a look at what happened to Little Bobby Tables. Even if you are escaping inputs, its not safe!. Use prepared parameterized statements instead.

increment variable on submit to update mysql query

I am new to PHP(loving it already)
I have a form that looks up a table that sends 'golf hole' info back and allows a golfer to input their score of the hole. Problem I have is that I can present the first hole by looking up the hole_detail table but then cant figure out how loop through the table for hole 2, 3.....18 when the form is submitted. I have searched stackoverflow but cant find anything that specific about it. I have tried an if statement, if (isset($_POST['Submit'])) to try increment the $hole_id. Am I completely going about it the wrong way? Thanks in advance.
<?php
include ('../scripts/dbconfig.php');
# get the most recent course name:
$get_course_name = mysql_query("SELECT course_name FROM comp ORDER BY PID DESC LIMIT 1");
$show_course_name = mysql_fetch_array($get_course_name);
if (isset($_POST['Submit'])) {
$hole_id =1;
else {
$hole_id = $hole_id + 1;
}
}
# get the hole yardage and SI from most recent selected golf course:
$get_course_detail = mysql_query("SELECT * FROM `course_detail` WHERE course_name = '". $show_course_name['course_name'] . "'");
$show_course_detail = mysql_fetch_array($get_course_detail);
$get_hole_detail = mysql_query("SELECT * FROM `course_detail`,`phoenix_hole` WHERE Course_ID = 6 AND hole_id = $hole_id");
$show_hole_detail = mysql_fetch_array($get_hole_detail);
?>
</head>
<body>
<table width="300" cellspacing="0" cellpadding="0">
<tr>
<td width="40"><?php echo $show_course_name['course_name'];?></td>
</tr>
<tr>
<td width="20">HOLE <?php echo $show_hole_detail['hole_id']?></td>
<td width="5"> PAR <?php echo $show_hole_detail['hole_par'];?></td>
</tr>
<tr>
<td width="20">Yards</td>
<td width="20">S.I</td>
</tr>
<tr>
<td bgcolor="yellow"><?php echo $show_hole_detail['yellow_yards'];?></td>
<td><?php echo $show_hole_detail['hole_si'];?></td>
</tr>
<tr>
<td border="1px" bgcolor="white"><?php echo $show_hole_detail['white_yards'];?></td>
<td><?php echo $show_hole_detail['hole_si'];?></td>
</tr>
<tr>
<td bgcolor="red"><?php echo $show_hole_detail['red_yards'];?></td>
<td><?php echo $show_hole_detail['hole_si'];?></td>
</tr>
</table>
</p>
<form id="game_form" name="game_form" method="post" action="game_form.php">
<table width="300" border="0" align="left" cellpadding="2" cellspacing="0">
<tr>
<td><b>Hole Shots</b></td>
<td><input name="hole_shots" type="text" class="textfield" id="hole_shots" maxlength="2" size="3" ></td>
<td><b>Putts</b></td>
<td><input name="putts" type="text" class="textfield" id="putts" maxlength="2" size="3"></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" value="Next Hole" align="center" /></td>
</tr>
</table>
</form>
</body>
</html>
Or you can use a hidden field that keeps the hole number and you can increment it from php.
$hole_id, in this scenario, will always be 1, because when a user clicks the Submit button, $_POST['Submit'] will always have a value. What you should do instead is have $_POST['Submit'] contain the value of $hole + 1. PHP is not going to "remember" what $hole_id was last time around; it's up to you to remind it. As soon as a request is sent to the browser--unless you're using sessions--PHP forgets everything about that request (HTTP is "stateless").
<?php
if (isset($_POST['Submit'])) {
$hole_id = (int)$_POST['Submit'];
} else {
$hole_id = 1;
}
# other code here
?>
You are on hole #<?php echo $hole_id; ?>.
<form>
<!-- form stuff here -->
<button type="submit" name="Submit" value="<?php echo $hole_id + 1; ?>">Next hole</button>
</form>

Blank screen when updating data in php table

I have been working on a project and i am at the final stages of the project. My problem is whenever i try to update data in my database table into returns a blank screen with no error messages. Please find the php script and html form (the form responsible for updating the database table) below, i have divided it into about four sections:
Thanks in advance
Update Form:
<a name="inventoryEditForm" id="inventoryEditForm"></a>
<h3>↓Add New Question Form↓</h3>
<form action="inventory_edit.php" enctype="multipart/from-data" name="myForm" id="myForm" method="post">
<table width="80%" border="0" cellspacing="3" cellpadding="7">
<tr>
<td width="20%"> </td>
<td width="80%"> </td>
</tr>
<tr>
<td>Question</td>
<td><textarea rows="" name="question" cols=""><?php echo $question; ?></textarea></td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td>Venue</td>
<td><input type="text" name="venue" maxlength="50" value="<?php echo $venue; ?>"></td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td>Date</td>
<td><input type="date" name="questiondate" value="<?php echo $date; ?>"></td>
</tr>
</table>
<br>
<input name="thisID" type="hidden" value="<?php echo $targetID; ?>"/>
<input type="submit" name="submit" value="Update Question">
<input type="reset" name="clear" value="Clear Form">
</form>
PHP Script:
<?php
//Error reporting due to long script
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<?php
error_reporting(E_PARSE);
//Update question table
If (isset($_POST['question'])) {
$id = mysqli_real_escape_string($link, $_POST['thisID']);
$question = mysqli_real_escape_string($link, $_POST['question']);
$venue = mysqli_real_escape_string($link, $_POST['venue']);
$date = mysqli_real_escape_string($link, $_POST['questiondate']);
//Update question in the table
$sql = mysqli_query($link, "UPDATE DebateQuestion SET question='$question',venue='$venue',date='$date' WHERE qQuestionNo='$id'LIMIT 1") or die(mysql_error());
header("location: inventory.php");
exit();
}
?>
<?php
error_reporting(E_PARSE);
//Gather this questions full information and insert automatically into the edit form
if (isset($_GET['qid'])) {
$targetID = $_GET['qid'];
$sql = mysqli_query($link, "SELECT * FROM DebateQuestion WHERE qQuestionNo='$targetID'LIMIT 1") or die(mysql_error());
$questionCount = mysqli_num_rows($sql); // count the output amount
if ($questionCount > 0) {
while ($row = mysqli_fetch_array($sql, MYSQLI_ASSOC)) {
$id = $row["qQuestionNo"];
$question = $row["qQuestion"];
$venue = $row["qDebateVenue"];
$date = strftime("%b %d, %Y", strtotime($row["qDate"]));
}
} else {
echo "Oops, no questions like that exists. Check <a href='inventory.php'>inventory</a>again";
exit();
}
}
?>
In your update query you have the data column without using ` back ticks , date is also a mysql's function try to wrap up your column names with back ticks if you are not sure whether they conflict with mysql's reserved keywords
$sql = mysqli_query($link,"UPDATE DebateQuestion SET
`question`='$question',`venue`='$venue',`date`='$date'
WHERE qQuestionNo='$id'LIMIT 1")
"SELECT * FROM DebateQuestion WHERE qQuestionNo='$targetID'LIMIT 1"
Here is qQuestionNo column a string type?if not remove quotes around $targetID.
Note : I have not tested the code - just read it on screen.
I've never seen an IF statement capitalized before :
If (isset($_POST['question'])) {
I'd guess this makes a difference however.
There's lots of other weird things going on in your files, but none that should give you white screen. Try lowercase 'I' in your if statement first.
ALSO - re: the UPDATE statement, you are missing a space between the $id and the LIMIT :
**qQuestionNo='$id'LIMIT 1**

Inserting data into MySQL via PHP, unidentified index and i have defined them?

I'm trying to set up a webshop for a school project, and i'm having trouble with the order section. I have defined all my variable, to my knowlegde atleast, but it still says "unidentified index".
Here's the form part of the html code (The name's are in Danish, just ignore those)
<div id="bestillingform">
<table align="center" border="0">
<form name="bestilling" action="insert.php" method="post">
<tr id="headline">
<th colspan="2" align="center"><p>Bestilling</p></th>
</tr>
<tr>
<td>Fornavn: </td>
<td><input type="text" name="fornavn" ><br/></td>
</tr>
<tr>
<td>Efternavn: </td>
<td><input type="text" name="efternavn" ><br/></td>
</tr>
<tr>
<td>Land: </td>
<td><input type="text" name="land" ><br/></td>
</tr>
<tr>
<td>By: </td>
<td><input type="text" name="by" ><br/></td>
</tr>
<tr>
<td>Pickup sted: </td>
<td><input type="text" name="pickup" ><br/></td>
</tr>
<tr>
<td>Antal personer: </td>
<td><input type="text" name="antal" ><br/></td>
</tr>
<tr>
<td>Pakke: </td>
<td><input type="text" name="pakke" ><br/></td>
</tr>
<tr>
<td>Kodeord: </td>
<td><input type="password" name="kodeord" ><br/></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submit" value="Bestil" ><br/></td>
</tr>
</form>
</table>
</div>
and here's my php code:
$host="localhost";
$user="root";
$password="";
$database="webshop";
$table="bestilling";
$fnavn = $_POST['fornavn'];
$enavn = $_POST['efternavn'];
$land = $_POST['land'];
$by = $_POST['by'];
$pickup = $_POST['pickup'];
$antal = $_POST['antal'];
$pakke = $_POST['pakke'];
$kodeord = $_POST['kodeord'];
// Forbindelse til server
mysql_connect($host, $user, $password)or die("Kan ikke forbinde til server");
mysql_select_db($database)or die("Kan ikke forbinde til databasen");
$bestil = "INSERT INTO $table(Fornavn, Efternavn, Land, By, Pickup, Antal, Pakke, Kodeord)VALUES('$fnavn', '$navn', '$land', '$by', '$pickup', '$antal', '$pakke', '$kodeord')";
$result = mysql_query($bestil);
if ($result){
echo("<br>Din bestilling er blevet oprettet");
}else{
echo(mysql_error());
}
mysql_close();
?>
I have also tried to post data manually into the table and see if i could retrive them, to check if i had a connection to the database at all - but i didn't have any problems with that, so i'm at a dead end here.
You use $navn in your SQL query, but the variable is actually called $enavn.
$bestil = "INSERT INTO $table(Fornavn, Efternavn, Land, By, Pickup, Antal, Pakke, Kodeord)VALUES('$fnavn', '$enavn', '$land', '$by', '$pickup', '$antal', '$pakke', '$kodeord')";
Change one of these.
The keyword 'by' is a mySQL reserved keyword, and it is one of your field describe in your INSERT statement. You have to surrend it with `by`.
I will suggest you to use id in input tag, like
<input type="text" name="fornavn" id="fornavn" />
Then you will get the value in $_POST['fornavn''].

Categories