I have a form which will submit information that inserts a row into a database (company details). The submission works ok but I also find that going to the page containing the form will insert a blank row in the database. Any idea why this may be? Code is as below:
<?php
echo "<form action='addnew.php' method='post'>";
echo "<input type='text' name='categoryAdd' value='Travel'/><br/>";
echo "<input type='text' name='EstablishmentNameAdd' value='EstablishmentName'/><br/>";
echo "<input type='text' name='Address1Add' value='Address1'/><br/>";
echo "<input type='text' name='Address2Add' value='Address2'/><br/>";
echo "<input type='text' name='Address3Add' value='Address3'/><br/>";
echo "<input type='text' name='Address4Add' value='Address4'/><br/>";
echo "<input type='text' name='PostcodeAdd' value='Postcode'/><br/>";
echo "<input type='text' name='NearestStationAdd' value='NearestStation'/><br/>";
echo "<input type='text' name='TelAdd' value='Tel'/><br/>";
echo "<input type='text' name='FaxAdd' value='Fax'/><br/>";
echo "<input type='text' name='EmailAdd' value='Email'/><br/>";
echo "<input type='text' name='WebsiteAdd' value='Website'/><br/>";
echo "<input type='text' name='DescriptionAdd' value='Description'/><br/>";
echo "<input type='submit' value='test'/>";
echo "</form>";
$EstablishmentNameAdd = mysql_real_escape_string($_POST['EstablishmentNameAdd']);
$CategoryAdd = $_POST['categoryAdd'];
$Address1Add = mysql_real_escape_string($_POST['Address1Add']);
$Address2Add = mysql_real_escape_string($_POST['Address2Add']);
$Address3Add = mysql_real_escape_string($_POST['Address3Add']);
$Address4Add = mysql_real_escape_string($_POST['Address4Add']);
$PostcodeAdd = $_POST['PostcodeAdd'];
$NearestStationAdd = mysql_real_escape_string($_POST['NearestStationAdd']);
$TelAdd = $_POST['TelAdd'];
$FaxAdd = $_POST['FaxAdd'];
$EmailAdd = mysql_real_escape_string($_POST['EmailAdd']);
$WebsiteAdd = mysql_real_escape_string($_POST['WebsiteAdd']);
$DescriptionAdd = mysql_real_escape_string($_POST['DescriptionAdd']);
$result1 = mysql_query("INSERT into establishment_id (EstablishmentName) values ('$EstablishmentNameAdd')");
$result2 = mysql_query("SELECT * from establishment_id where EstablishmentName = '$EstablishmentNameAdd'");
while($row = mysql_fetch_array($result2))
{
$ID = $row['EstablishmentID'];
$NAME = $row['EstablishmentName'];
};
$result3 = mysql_query("INSERT into establishmentdetails (EstablishmentID, EstablishmentName, category, Address1, Address2, Address3, Address4, Postcode, NearestStation, Tel, Fax,
Email, Website, Description) values('$ID', '$EstablishmentNameAdd', '$CategoryAdd', '$Address1Add', '$Address2Add', '$Address3Add', '$Address4Add', '$PostcodeAdd', '$NearestStationAdd', '$TelAdd', '$FaxAdd', '$EmailAdd',
'$WebsiteAdd', '$DescriptionAdd')");
?>
Add this after all the echos:
if(!empty($_POST)){
...
}
Wrap all the bottom code in that, so that way only if there is a post it will do the insert.
This is happening because you are echoing out the form.. and then immediately inserting things into your database that don't exist yet.
You can try this:
if(count($_POST) > 0 && in_array('EstablishmentNameAdd',$_POST)) {
$EstablishmentNameAdd = mysql_real_escape_string($_POST['EstablishmentNameAdd']);
$CategoryAdd = $_POST['categoryAdd'];
$Address1Add = mysql_real_escape_string($_POST['Address1Add']);
$Address2Add = mysql_real_escape_string($_POST['Address2Add']);
$Address3Add = mysql_real_escape_string($_POST['Address3Add']);
$Address4Add = mysql_real_escape_string($_POST['Address4Add']);
$PostcodeAdd = $_POST['PostcodeAdd'];
$NearestStationAdd = mysql_real_escape_string($_POST['NearestStationAdd']);
$TelAdd = $_POST['TelAdd'];
$FaxAdd = $_POST['FaxAdd'];
$EmailAdd = mysql_real_escape_string($_POST['EmailAdd']);
$WebsiteAdd = mysql_real_escape_string($_POST['WebsiteAdd']);
$DescriptionAdd = mysql_real_escape_string($_POST['DescriptionAdd']);
$result1 = mysql_query("INSERT into establishment_id (EstablishmentName) values ('$EstablishmentNameAdd')");
$result2 = mysql_query("SELECT * from establishment_id where EstablishmentName = '$EstablishmentNameAdd'");
while($row = mysql_fetch_array($result2))
{
$ID = $row['EstablishmentID'];
$NAME = $row['EstablishmentName'];
};
$result3 = mysql_query("INSERT into establishmentdetails (EstablishmentID, EstablishmentName, category, Address1, Address2, Address3, Address4, Postcode, NearestStation, Tel, Fax,
Email, Website, Description) values('$ID', '$EstablishmentNameAdd', '$CategoryAdd', '$Address1Add', '$Address2Add', '$Address3Add', '$Address4Add', '$PostcodeAdd', '$NearestStationAdd', '$TelAdd', '$FaxAdd', '$EmailAdd',
'$WebsiteAdd', '$DescriptionAdd')");
}
Related
<?php
$DatabaseServer = "localhost";
$DatabaseUsername = "root";
$DatabasePassword = "root";
$DatabaseName = "demo";
$Connection = mysqli_connect($DatabaseServer, $DatabaseUsername, $DatabasePassword, $DatabaseName);
if ($Connection === false) {
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$sqlusers = "select * from user";
$result = mysqli_query($Connection, $sqlusers);
echo "<form method='POST'>";
while($rowuser = mysqli_fetch_array($result)){
$user = $rowuser['FirstName'];
echo "<input type='text' name='firstName' value='$user' readonly>";
echo "<select name='attendanceType'>";
$sqltype = "select * from attendancetype";
$resultaType = mysqli_query($Connection, $sqltype);
while($rowtype = mysqli_fetch_array($resultaType)){
echo "<option>";
echo $rowtype['name'];
echo "</option>";
}
echo "</select>";
echo "<br>";
}
echo "<input type='submit' name='submit' value='submit'>";
echo "</form>";
?>
Users table.
INSERT INTO `user` (`UserID`, `FirstName`, `LastName`, `Email`, `Password`, `City`) VALUES
(7, 'Rahul', 'Rajshekaran', 'Rahul#zzz.xxx', 'Rahul#123', 'Pune'),
(8, 'Mahesh', 'Krishna', 'Mahesh#xxx.xxx', 'Mahesh#123', 'Delhi');
attendancetype table:
INSERT INTO `attendancetype` (`attendanceTypeID`, `name`) VALUES
(0001, 'Present'),
(0002, 'Absent');
How can I inserting data into table on a single submission of form?
your code has one mistake. If you are looping form elements, you must achieve, that every input has the unique name.
$i = 0;
while($rowuser = mysqli_fetch_array($result)){
$user = $rowuser['FirstName'];
echo "<input type='text' name='firstName[".$i."]' value='$user' readonly>";
echo "<select name='attendanceType[".$i."]'>";
$sqltype = "select * from attendancetype";
$resultaType = mysqli_query($Connection, $sqltype);
while($rowtype = mysqli_fetch_array($resultaType)){
echo "<option>";
echo $rowtype['name'];
echo "</option>";
}
echo "</select>";
echo "<br>";
$i++;
}
process form with:
<?php
mysqli_set_charset($Connection, "utf8");
foreach($_POST['firstName'] as $i => $user) {
$sql = "insert into table set attendance_type = '".mysqli_real_escape_string($Connection, $_POST['attendanceType'][$i])."' where user='".mysqli_real_escape_string($Connection, $user)."'";
mysqli_query($Connection, $sql);
}
?>
escapeFunction is used as a refferer to the fact, that you should escape somehow (there are more ways) every input and it needs to be replaced or defined
I have a "racing" website where I am recording racers results, but I am trying to enter their results in all at once. shown below is the code
Your problem is that you believe when you have multiple <input name="" /> you think that creates a array $_POST[], that is not true...
while ($row = mysql_fetch_assoc($result))
{
$reID = $row['reID'];
$racerID = $row['racerID'];
echo "<tr>";
echo "<td>$reID<input type='hidden' name='reID' value='$reID'>";
echo "<td>$racerID<input type='hidden' name='racerID' value='$racerID'>";
echo"<td><input type='text' name='rank'>";
echo"<td><input type='text' name='timetaken'>";
}
you are over writing the reID, racerID, rank, and time taken every time it is printed out... by default it takes the last one.
what you could do to solve this is:
$count = 0;
while ($row = mysql_fetch_assoc($result))
{
$count++;
$reID = $row['reID'];
$racerID = $row['racerID'];
echo "<tr>";
echo "<td>$reID<input type='hidden' name='reID$count' value='$reID'>";
echo "<td>$racerID<input type='hidden' name='racerID$count' value='$racerID'>";
echo"<td><input type='text' name='rank$count'>";
echo"<td><input type='text' name='timetaken$count'>";
}
echo "<input type='hidden' name='count' value='$count' />";
That way each value is unique...
Then instead of this:
$rank=$_POST['rank'];
$timetaken=$_POST['timetaken'];
$reID=$_POST['reID'];
$racerID=$_POST['racerID'];
$count_racerID= count($_POST['racerID']);
do this
$count = $_POST['count'];
for($i=1; $i<=$count; $i++){
$rank[] = $_POST['rank'.$i];
$timetaken[] = $_POST['timetaken'.$i];
$reID[] = $_POST['reID'.$i];
$racerID[] = $_POST['racerID'.$i];
}
$count_racerID = $count;
now you have arrays!
For your SQL statement...
$sql = mysql_query("INSERT INTO RaceResults (rank, timetaken, reID, racerID) VALUES ('$rank', '$timetaken', '$reID', '$racerID')");
should be
$sql = mysql_query("INSERT INTO RaceResults (rank, timetaken, reID, racerID) VALUES ('$_rank', '$_timetaken', '$_reID', '$_racerID')");
The final product should be something like this:
The input form:
$reID = $_GET['reID'];
$result = mysql_query("SELECT * FROM RaceEventRacer WHERE reID = $reID");
$count = 0;
while ($row = mysql_fetch_assoc($result))
{
$count++;
$reID = $row['reID'];
$racerID = $row['racerID'];
echo "<tr>";
echo "<td>$reID<input type='hidden' name='reID$count' value='$reID'>";
echo "<td>$racerID<input type='hidden' name='racerID$count' value='$racerID'>";
echo"<td><input type='text' name='rank$count'>";
echo"<td><input type='text' name='timetaken$count'>";
}
echo "<input type='hidden' name='count' value='$count' />";
and the SQL should be:
$count = $_POST['count'];
for($i=1; $i<=$count; $i++){
$rank[] = $_POST['rank'.$i];
$timetaken[] = $_POST['timetaken'.$i];
$reID[] = $_POST['reID'.$i];
$racerID[] = $_POST['racerID'.$i];
}
$count_racerID = $count;
for($i=0;$i<$count_racerID;$i++){
$_rank= mysql_escape_string($rank[$i]);
$_timetaken= mysql_escape_string($timetaken[$i]);
$_reID= mysql_escape_string($reID[$i]);
$_racerID= mysql_escape_string($racerID[$i]);
$sql = mysql_query("INSERT INTO RaceResults (rank, timetaken, reID, racerID) VALUES ('$_rank', '$_timetaken', '$_reID', '$_racerID')");
$result = mysql_query($sql);
}
I have 2 textbox array,
questions & correct answer...
the problem is , i cannot save it to the the database
questions should be saved to "test" field in database
and correct answer should be saved to "test2" field...
code for the textbox
echo "<label for='textfield[]' align='left'> Question </label>";
echo "<br/>";
echo "<input type='text' name='textfield[]'>";
echo "<br/>";
echo "<label for='textfield2[]' align='left'> Correct Answer </label>";
echo "<br/>";
echo "<input type='text' name='textfield2[]'>";
echo "<br/>";
code for inserting values in database
$sql = array();
foreach($_POST['textfield'] as $textfield){
foreach($_POST['textfield2'] as $textfield2){
$sql[] = "INSERT INTO practice (test,test2) VALUES ('{$textfield}','{$textfield2}')";
}
}
foreach($sql as $query){
mysqli_query($con,$query);
}
}
You can insert like this,
<?php
$post_count = count($_POST['textfield']);
$post1 = array();
$post2 = array();
$post1 = $_POST['textfield'];
$post2 = $_POST['textfield2'];
for ($i = 0; $i <= $post_count; $i++) {
$sql[] = "INSERT INTO practice (test,test2) VALUES ('".$post1[$i]."','".$post2[$i]."')";
}
foreach ($sql as $query) {
mysqli_query($con, $query);
}
?>
$sql = "SELECT distinct s.doc_id, s.pat_id as pat_id, p.pat_fullname as fullname, p.pat_id from patient p, patientscript s WHERE s.doc_id = '$doc_id' AND s.status = '1' AND s.pat_id = p.pat_id;";
$result = mysql_query($sql) OR
die("Database Error. MYSQL-Error:".mysql_error()."\n");
echo "<form name='form'> ";
echo "<label>Patient :</label>";
echo "<select name='patname'>";
echo "<option>Select a patient</option>";
while ($row = mysql_fetch_array($result))
{
$patname = $row['fullname'];
$pat_id = $row['pat_id'];
echo "<option value='$patname'>$patname</option>";
}
echo "</select>";
echo "<input type='button' value='Submit' onClick='get();' >";
echo "<input type='hidden' name='pat_id' value='$pat_id'/>";
echo "</form>";
echo "<div id='showName'></div>";
Let say, there are 2 echo results from option value, A and B.
When select A, I get 12(id) A from output
When select B, I get 12(id) B from output
Actually the 12 is for B, A is 7, anyone can help me solve this problem.
Try this:
(...)
echo "<select name='patdata'>";
echo "<option>Select a patient</option>";
while ($row = mysql_fetch_array($result))
{
$patname = $row['fullname'];
$pat_id = $row['pat_id'];
echo "<option value='".$patname."-".$pat_id."'>$patname</option>";
}
echo "</select>";
echo "<input type='button' value='Submit' onClick='get();' >";
echo "</form>";
(...)
Then, (I'm assuming you are fetching the results after post)
$patdata = explode( '-', $_REQUEST['patdata']);
$patname = $patdata[0];
$pat_id = $patdata[1];
I got a php form to update MySQL tables. The fetch works perfectly but the updates are not working. This is my form code :
<?php
$sql= "SELECT client.resID AS resID, client.resName AS resName FROM client WHERE client.resID =".$_GET["resID"];
$rs = mysql_query($sql) or die($sql."<br/><br/>".mysql_error());
$sqlM= "SELECT menu.id AS mid, menu.name AS mname FROM menu WHERE menu.resID =".$_GET["resID"];
$rsM = mysql_query($sqlM) or die($sqlM."<br/><br/>".mysql_error());
$sqlF= "SELECT facilities.id AS fid, facilities.name AS fname FROM facilities WHERE facilities.resID =".$_GET["resID"];
$rsF = mysql_query($sqlF) or die($sqlF."<br/><br/>".mysql_error());
$sqlS= "SELECT services.id AS sid, services.name AS sname FROM services WHERE services.resID =".$_GET["resID"];
$rsS = mysql_query($sqlS) or die($sqlS."<br/><br/>".mysql_error());
// $names array now contains all names
$i = 0;
echo '<table width="50%">';
echo '<tr>';
echo '<td>ID</td>';
echo '<td>Name</td>';
echo '<td>Edit</td>';
echo '</tr>';
echo "<form name='form_update' method='post' action='client_admin_post.php'>\n";
while ($fm = mysql_fetch_array($rsM)) { // loop as long as there are more results
$mnames[] = $fm['mname'];
$mid[] = $fm['mid']; // push to the array
echo '<tr>';
echo "<td>Menu :</td>";
echo "<td><input type='text' size='40' name='mname' value='{$fm['mname']}' /></td>";
echo "<td>{$fm['id']}<input type='hidden' name='mid' value='{$fm['mid']}' /></td>";
echo '</tr>';
++$i;
print_r($mnames);
}
while ($ff = mysql_fetch_array($rsF)) { // loop as long as there are more results
$fnames[] = $ff['fname'];
$fid[] = $ff['fid']; // push to the array
echo '<tr>';
echo "<td>Facilities :</td>";
echo "<td><input type='text' size='40' name='fname' value='{$ff['fname']}' /></td>";
echo "<td>{$ff['id']}<input type='hidden' name='fid' value='{$ff['fid']}' /></td>";
echo '</tr>';
++$i;
}
while ($fs = mysql_fetch_array($rsS)) { // loop as long as there are more results
$snames[] = $fs['sname'];
$sid[] = $fs['sid']; // push to the array
echo '<tr>';
echo "<td>Services :</td>";
echo "<td><input type='text' size='40' name='sname' value='{$fs['sname']}' /></td>";
echo "<td>{$fs['id']}<input type='hidden' name='sid' value='{$fs['sid']}' /></td>";
echo '</tr>';
++$i;
}
echo'<tr>
<td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>';
?>
This is my post code :
$size = count($_POST['mname']);
$i = 0;
while ($i < $size) {
$mname= $_POST['mname'][$i];
$mid = $_POST['mid'][$i];
$query = "UPDATE menu SET name = '$mname' WHERE id = '$mid' LIMIT 1";
mysql_query($query) or die ("Error in query: $query");
echo "$mname<br /><br /><em>Updated!</em><br /><br />";
++$i;
}
$size = count($_POST['fname']);
$i = 0;
while ($i < $size) {
$fname= $_POST['fname'][$i];
$fid = $_POST['fid'][$i];
$query1 = "UPDATE facilities SET name = '$fname' WHERE id = '$fid' LIMIT 1";
mysql_query($query1) or die ("Error in query: $query1");
echo "$fname<br /><br /><em>Updated!</em><br /><br />";
++$i;
}
$size = count($_POST['sname']);
$i = 0;
while ($i < $size) {
$sname= $_POST['sname'][$i];
$sid = $_POST['sid'][$i];
$query3 = "UPDATE services SET name = '$sname' WHERE id = '$sid' LIMIT 1";
mysql_query($query3) or die ("Error in query: $query3");
echo "$sname<br /><br /><em>Updated!</em><br /><br />";
++$i;
}
I got 'updated' status in post page but nothing is updated in MySQL table. How to solve this problem? Really appreciate your help :D
You're creating multiple inputs with the same name, such as "fname," but you're trying to access them as if they were an array. Rename the fields like the following:
echo "<td><input type='text' size='40' name='mname[]' value='name' /></td>";
too long question but if you but by update status in post page you mean to say it echoed update if i am right it will echo update since you are checking it in while and the condition is true. But have you echoed the posted values in post page check whether you are getting the value proceed this way to find where are you having problems before asking here long questions.
you can use transacrions in mysql if you want all the queries should be executed otherwise none.
Your post page says updated because at the end of your queries you just echo "Updated", there is no condition when it should be printed.
and your data is not updated as you are not specifying any connection variable for mysql_query("query",$conn)