Values not being submitted from PHP HTML form [closed] - php

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 5 years ago.
Improve this question
I am trying to take the input from drop down menus that users enter and submit them to a table in my database. I am trying to submit the values into this table:
I use the POST to check that the values are being pulled from the HTML form and they are, but they won't submit into my table. I've made sure that all of the names with the columns and HTML forms are correct, why won't the values post to the table?
<?php
$databaseName = 'pizza_db';
$databaseUser = 'root';
$databasePassword = 'root';
$databaseHost = '127.0.0.1';
$conn = new mysqli($databaseHost, $databaseUser, $databasePassword, $databaseName);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected sucessfully\n";
if(isset($_POST['submit'])){
$value = mysqli_real_escape_string($conn,$_POST['drink']);
$value2 = mysqli_real_escape_string($conn,$_POST['cheese']);
$value3 = mysqli_real_escape_string($conn,$_POST['veggies']);
$value4 = mysqli_real_escape_string($conn,$_POST['meat']);
$value5 = mysqli_real_escape_string($conn,$_POST['sauce']);
$value6 = mysqli_real_escape_string($conn,$_POST['crust']);
$value7 = mysqli_real_escape_string($conn,$_POST['size']);
$sql = "INSERT INTO order_info(drink,cheese,veggies,meat,sauce,crust,size)
VALUES('$value','$value2','$value3','$value4','$value5','$value6','$value7')";
//Here I am posting the values to check that they are being submitted
echo $_POST["size"];
echo "\n";
echo $_POST["sauce"];
echo "\n";
echo $_POST["crust"];
echo "\n";
echo $_POST["cheese"];
echo "\n";
echo $_POST["meat"];
echo "\n";
echo $_POST["veggies"];
echo "\n";
echo $_POST["drink"];
$conn->close();
}
?>
<!DOCTYPE html>
<html>
<body>
<form action='' method='post'>
<p>Choose a size<p>
<select id="size" name="size">
<option value="small">Small</option>
<option value="medium">Medium</option>
<option value="large">Large</option>
<option value="x-large">X-large</option>
</select>
<p> Choose a sauce <p>
<select id="sauce" name="sauce">
<option value="none">None</option>
<option value="marinara">Marinara</option>
<option value="alfredo">Alfredo</option>
<option value="ranch">Ranch</option>
<option value="bbq">BBQ</option>
</select>
<p> Choose a cheese<p>
<select id="cheese" name="cheese">
<option value="none">None</option>
<option value="mozzarelaa">Mozarella</option>
<option value="cheddar">Cheddar</option>
<option value="parmesan">Parmesan</option>
<option value="three cheese">Three-Cheese</option>
</select>
<p> Choose a meat <p>
<select id="meat" name="meat">
<option value="none">None</option>
<option value="Pepperroni">Pepperroni</option>
<option value="sausage">Sausage</option>
<option value="bacon">Bacon</option>
<option value="canadian bacon">Canadian Bacon</option>
<option value="chicken">Chicken</option>
<option value="salami">Beef</option>
<option value="anchovies">Anchovies</option>
</select>
<p> Choose a veggies <p>
<select id="veggies" name="veggies">
<option value="none">None</option>
<option value="onions">Onions</option>
<option value="green peppers">Green Peppers</option>
<option value="Red peppers">Red peppers</option>
<option value="Black olives">Mushrooms</option>
<option value="jalapenos">Jalapenos</option>
<option value="tomatoes">Tomatoes</option>
<option value="pineapple">Pineapple</option>
</select>
<p> Choose a crust <p>
<select id="crust" name="crust">
<option value="regular">Regular</option>
<option value="deep-dish">Deep-dish</option>
<option value="thin-crust">Thin Crust</option>
<option value="stuffed crust">Stuffed Crust</option>
<option value="gluten free">Gluten Free</option>
</select>
<p> Choose a drink <p>
<select id="drink" name="drink">
<option value="none">None</option>
<option value="rootbeer">Root Beer</option>
<option value="coke">Coke</option>
<option value="diet coke">Diet Coke</option>
<option value="dr pepper">Dr Pepper</option>
</select>
<input type="submit" name="submit" value="Submit"/>
</form>
</body>
</html>

Seems like you are not running the query.
// sql
$sql = "INSERT INTO order_info(drink,cheese,veggies,meat,sauce,crust,size)
VALUES('$value','$value2','$value3','$value4','$value5','$value6','$value7')";
// run query
mysqli_query($conn, $sql);
// or
$conn->query($sql);

You prepared string query but you are not executing it.
$sql = "INSERT INTO order_info(drink,cheese,veggies,meat,sauce,crust,size)
VALUES('$value','$value2','$value3','$value4','$value5','$value6','$value7')";
// run query with below mentioned function
mysqli_query($conn, $sql);
Then check your table. You will see the data saved.

Related

<option> values showing as 0 in database

I cannot upload to the database drop down menu variables, when the form is submitted the text area is blank. I am new to coding and all of the information I have found thus far has been unable to help me. The students first and last names are submitting fine I just now need to process their grades
These are the Subjects:
English<br>
<select name="Grade">
<option value="-">-</option>
<option value="A*">A*</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="Fail">Fail</option>
</select><br>
Maths<br>
<select name="Grade2">
<option value="-">-</option>
<option value="A*">A*</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="Fail">Fail</option>
</select><br>
Science<br>
<select name="Grade3">
<option value="-">-</option>
<option value="A*">A*</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="Fail">Fail</option>
</select><br>
I need to upload them to here:
$DB_HOST = "localhost";
$DB_USERNAME = "admin";
$DB_PASSWORD = "chichester";
$DB_NAME = "results";
$fname = $_POST["fname"];
$lname = $_POST["lname"];
$examboard = $_POST["examboard"];
$grade = $_POST["grade"];
$grade2 = $_POST["grade2"];
$grade3 = $_POST["grade3"];
$additionalcomments = $_POST["Additional Comments"];
$conn = new mysqli($DB_HOST, $DB_USERNAME, $DB_PASSWORD, $DB_NAME);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO studentresults (Name,lastname,examboard,additionalcomments) VALUES ('$fname','$lname','$examboard','$grade','$grade2','$grade3','$additionalcomments')";
$sql = "INSERT INTO studentresults (grade, grade2, grade3) VALUES ('$grade','$grade2','$grade3')";
if ($conn->query($sql) === TRUE) {
echo "Student exam results have been successfully submitted. ";
} else {
echo "Error, please try again later. : " . $sql . "<br>" . $conn->error;
}
//close connection
$conn->close();
Now how do I overcome this, the script and database does not show an errors. All I need to do is process a series of exam results and display this in a database. The tag seems to have confused matters.
1) you are updating the insert query.
2) The query you have written is wrong as there number of columns is different than the number of values given.
3) $_POST["Additional Comments"]; this won't work, as array key cannot contain space.
Why do you have two $sql insert statements? Only use one.
INSERT INTO ___ (col1,col2) VALUES ('data1','data2');
You can't have spaces in $_POST['']; variables.

Php form (using PDO) to insert into other tables (foreign keys)

I need some help, I am trying to insert into multiple tables using PDO - Can someone see what I am doing wrong - I am not getting a parse errors (nor did I set up an asset error):
Here is my form:
addcontact.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Add New Contact</title>
<link rel="stylesheet" href="css/table.css" type="text/css" />
</head>
<body>
<div class="CSS_Table_Example" style="width:500px;height:350px;">
<center>
<form action="insert.php" method="post">
<p>
<td>
<tr><label for="ContactName">Contact Name:</label>
<input type="text" name="ContactName" id="ContactName">
</tr></p>
<p>
<tr> <label for="ContactTypeId">Contact Type:</label>
<select name="ContactTypeId">
<option value="1">Contact</option>
<option value="2">Organization</option>
</select>
</p>
<p>
<td>
<tr> <label for="AddressTypeId">Address Type:</label>
<select name="AddressTypeId">
<option value="1">Home</option>
<option value="2">Office</option>
<option value="3">Other</option>
</select>
</p>
<p>
<tr><label for="Address1">Address 1:</label>
<input type="text" name="Address1" id="Address1">
</tr></p>
<p>
<tr><label for="Address2">Address 2:</label>
<input type="text" name="Address2" id="Address1">
</tr></p>
<p>
<tr><label for="City">City:</label>
<input type="text" name="City" id="Address1">
</tr></p>
<tr> <label for="StateId">State:</label>
<select name="StateId">
<option value="1">Alabama</option>
<option value="2">Alaska</option>
<option value="3">Arizona</option>
<option value="4">Arkansas</option>
<option value="5">Califorina</option>
<option value="6">Colorado</option>
<option value="7">Connecticut</option>
<option value="8">Delaware</option>
<option value="9">District of Columbia</option>
<option value="10">Florida</option>
<option value="11">Georgia</option>
<option value="12">Hawaii</option>
<option value="13">Idaho</option>
<option value="14">Illinois</option>
<option value="15">Indiana</option>
<option value="16">Iowa</option>
<option value="17">Kansas</option>
<option value="18">Kentucky</option>
<option value="19">Louisana</option>
<option value="20">Maine</option>
<option value="21">Maryland</option>
<option value="22">Massachusetts</option>
<option value="23">Michigan</option>
<option value="24">Minnesota</option>
<option value="25">Mississippi</option>
<option value="26">Missouri</option>
<option value="27">Montana</option>
<option value="28">Nebraska</option>
<option value="29">Nevada</option>
<option value="30">New Hampshire</option>
<option value="31">New Jersey</option>
<option value="32">New Mexico</option>
<option value="33">New York</option>
<option value="34">North Carolina</option>
<option value="35">North Dakota</option>
<option value="36">Ohio</option>
<option value="37">Oklahoma</option>
<option value="38">Oregon</option>
<option value="39">Pennsylvania</option>
<option value="40">Rhode Island</option>
<option value="41">South Carolina</option>
<option value="42">South Dakota</option>
<option value="43">Tennessee</option>
<option value="44">Texas</option>
<option value="45">Utah</option>
<option value="46">Vermont</option>
<option value="47">Virginia</option>
<option value="48">Washington</option>
<option value="49">West Virginia</option>
<option value="50">Wisconsin</option>
<option value="51">Wyoming</option>
</select>
</tr> </p>
<input type="submit" value="Add Record">
</tr></td>
</form>
</table>
</body>
</html>
Here is insert.php
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "crm";
//making an array with the data received, to use as named placeholders for INSERT by PDO.
$data = array('ContactName' => $_POST['ContactName'] , 'ContactTypeId'
=> $_POST['ContactTypeId'],
'ContactId'=> $_POST['ContactId'],'AddressTypeId'=>
$_POST['AddressTypeId'],'Address1'=>$_POST['Address1'],
'Address2'=>$_POST['
Address2'],'City'=>$_POST['City'],'StateId'=>$_POST['StateId']);
try {
// preparing database handle $dbh
$dbh = new PDO("mysql:host=$servername;dbname=$dbname",
$username,$password);
// set the PDO error mode to exception
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$currentID = mysql_inserted_id();
// query with named placeholders to avoid sql injections
$query = "INSERT INTO Contacts (ContactName, ContactTypeId)
VALUES(:ContactName, :ContactTypeId )";
$query2= "INSERT INTO
Addresses(ContactId,AddressTypeId,Address1,Address2,City,StateId)
VALUES(:$currentID,:AddressTypeId,:Address1,:Address2,:City,:StateId)";
//statement handle $sth
$sth = $dbh->prepare($query);
$sth->execute($data);
echo "New record created successfully";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$dbh = null;
?>
You need to create two arrays $data for $query & $data1 for $query1 and need use $dbh->lastInsertId() for last id. Use the below code. I think it will work:
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "crm";
//making an array with the data received, to use as named placeholders for INSERT by PDO.
$data = array('ContactName' => $_POST['ContactName'] , 'ContactTypeId'
=> $_POST['ContactTypeId']);
$data1=array('AddressTypeId'=>$_POST['AddressTypeId'],'Address1'=>$_POST['Address1'],
'Address2'=>$_POST['
Address2'],'City'=>$_POST['City'],'StateId'=>$_POST['StateId']);
try {
// preparing database handle $dbh
$dbh = new PDO("mysql:host=$servername;dbname=$dbname",
$username,$password);
// set the PDO error mode to exception
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// query with named placeholders to avoid sql injections
$query = "INSERT INTO Contacts (ContactName, ContactTypeId)
VALUES(:ContactName, :ContactTypeId )";
$sth = $dbh->prepare($query);
$sth->execute($data);
$currentID = $dbh->lastInsertId();
$query2= "INSERT INTO
Addresses(ContactId,AddressTypeId,Address1,Address2,City,StateId)
VALUES($currentID,:AddressTypeId,:Address1,:Address2,:City,:StateId)";
$sth = $dbh->prepare($query2);
$sth->execute($data1);
//statement handle $sth
echo "New record created successfully";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$dbh = null;
?>

php script won't connect MySQL

This is the code. and for some reason I can't find out why it is not working.
As you can see I've added a test query to see if it affects any changes on my db but nope :(
The funny thing is that in another php script I have succeeded to connect the db and even added some records to it thru the php script. Can't find the problem, Thanks from advance.
BTW. as you can see I have already defined the var "month" as string "hey" and echo it in the end of the php script to see if it changes. but nothing is happen!!
<form action="" name="form" id="form">
<label>
Select the month which you want to display its days.
<select name="month" form="form" required>
<option value="january">January</option>
<option value="february">February</option>
<option value="march">March</option>
<option value="april">April</option>
<option value="may">May</option>
<option value="june">June</option>
<option value="july">July</option>
<option value="august">August</option>
<option value="september">September</option>
<option value="october">October</option>
<option value="november">November</option>
<option value="december">December</option>
</select>
</label>
<input type="submit" name="update" value="Display" />
</form>
<?php
$month = "hey";
if(isset($_POST["update"]))
{
$month = $_POST["month"];
$query = "SELECT * FROM `days` WHERE `month`='{$month}';";
$conn = mysqli_connect("localhost","root","","db123");
$result = mysqli_query($conn,$query);
mysqli_query($conn,"INSERT INTO `days`(`month`,`day`) VALUES ('test','10');");
if($result)
{
die("Sorry!");
}
while($row = mysqli_fetch_row($result))
{
echo $month;
print_r($row);
}
mysqli_close($conn);
echo $month;
}
?
You didn't specified the method , its GET by default if.Change this line
if(isset($_POST["update"]))
to this
if(isset($_GET["update"]))
. Or if you want to use method as POST than just specify the method as POST
use the code below
<form action="" name="form" id="form" method="POST">
<label>
Select the month which you want to display its days.
<select name="month" form="form" required>
<option value="january">January</option>
<option value="february">February</option>
<option value="march">March</option>
<option value="april">April</option>
<option value="may">May</option>
<option value="june">June</option>
<option value="july">July</option>
<option value="august">August</option>
<option value="september">September</option>
<option value="october">October</option>
<option value="november">November</option>
<option value="december">December</option>
</select>
</label>
<input type="submit" name="update" value="Display" />
</form>
<?php
$month = "hey";
if(isset($_POST["update"]))
{
$month = $_POST["month"];
$query = "SELECT * FROM `days` WHERE `month`='{$month}';";
$conn = mysqli_connect("localhost","root","","db123");
$result = mysqli_query($conn,$query);
mysqli_query($conn,"INSERT INTO `days`(`month`,`day`) VALUES ('test','10');");
if($result)
{
die("Sorry!");
}
while($row = mysqli_fetch_row($result))
{
echo $month;
print_r($row);
}
mysqli_close($conn);
echo $month;
}
?>
Hope this helps you
<?php
$mysqli = new mysqli("localhost", "root", "", "db123");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
$month = "hey";
if(isset($_POST["update"]))
{
$month = $_POST["month"];
$res = $mysqli->query("SELECT * FROM `days` WHERE `month`='{$month}'");
mysqli_query($conn,"INSERT INTO `days`(`month`,`day`) VALUES ('test','10');");
while($row = $res->num_rows)
{
echo $month;
print_r($row);
}
mysqli_close($conn);
echo $month;
}
?>

PHP Add value to each table row data

i'm working on a php script wherein i must add certain score value at each row. I was able to display all the rows but i'm not sure on how would I able to store each of the given score in a variable and what query should I make to add all of them.
Here's my code
<?php
echo '<html>';
?>
<body>
<table border=0 cellspacing=0 cellpadding=0>
<?php
$connect = mysql_connect('localhost', 'root', '');
$db = 'labs';
$tb = 'comments';
$seldb = mysql_select_db($db, $connect);
echo '<form method="POST" action="..'.$_SERVER["PHP_SELF"].'">';
$query = mysql_query('SELECT com_id, comments FROM comments ORDER BY com_id ASC');
while($i = mysql_fetch_assoc($query)) {
echo'<tr><td>'.$i['comments'].'</td>';
echo'<td><select name="score" id="score" size="1">
<option value="5">5</option>
<option value="10">10</option>
<option value="15">15</option>
<option value="20">20</option>
<option value="25">25</option>
<option value="30">30</option>
<option value="35">35</option>
<option value="40">40</option>
<option value="45">45</option>
<option value="50">50</option>
</select></td></tr>';
echo'<br>';
}
echo'<input type="submit" name="submit" value="submit">';
echo'</form>';
if(isset($_POST['submit'])) {
//not sure if all the scores will be stored in here.
$id = $_POST['id'];
$query = mysql_query('insert query here');
}
?>
</table>
</body>
</html>
any suggestions are appreciated. Thanks in advance.:D
I think you need the id of each changed row (maybe as a hidden field for each row. Then just do a loop through all received rows and UPDATE each one.
You might also want to change all of your form field names to use the array format. This way it's easier to make your PHP loop throught them.
Sample row:
echo'<tr><td>'.$i['comments'].'</td>';
echo'<td><select name="score['.$i["id"].']" id="score" size="1">
<option value="5">5</option>
<option value="5">10</option>
<option value="5">15</option>
<option value="5">20</option>
<option value="5">25</option>
<option value="5">30</option>
<option value="5">35</option>
<option value="5">40</option>
<option value="5">45</option>
<option value="5">50</option>
</select></td></tr>';
Now just loop through the $_POST["score"] array and use the appropriate ID for your update.
foreach($_POST["score"] as $id => $value{
// ESCAPE your db values!!!!!
// query stuff with $value and $id
}
Also keep in Mind
mysql is deprecated! Use mysqli
Escape anything from outside sources like $_POST before use in SQL
You just needs to make an array of your drop down box like below,
while($i = mysql_fetch_assoc($query)) {
echo'<tr><td>'.$i['comments'].'</td>';
echo'<td><select name="score[".$i['com_id']."]" id="score" size="1">
<option valyue="5">5</option>
<option valyue="5">10</option>
<option valyue="5">15</option>
<option valyue="5">20</option>
<option valyue="5">25</option>
<option valyue="5">30</option>
<option valyue="5">35</option>
<option valyue="5">40</option>
<option valyue="5">45</option>
<option valyue="5">50</option>
</select></td></tr>';
echo'<br>';
}
and you can access it for all of your comments
<option valyue="5">50</option>
should be
<option value="5">50</option>
To send the value of a comment to database you need to add a ID of the comment
you should loop something like this.
$query = mysql_query('SELECT com_id, comments FROM comments ORDER BY com_id ASC');
while($i = mysql_fetch_assoc($query)) {
echo '<form method="POST" action="..'.$_SERVER["PHP_SELF"].'">';
echo '<input type="hidden" name="id" value="'.$i['com_id'].'">';
echo'<tr><td>'.$i['comments'].'</td>';
echo'<td><select name="score" id="score" size="1">
<option value="5">5</option>
<option value="5">10</option>
<option value="5">15</option>
<option value="5">20</option>
<option value="5">25</option>
<option value="5">30</option>
<option value="5">35</option>
<option value="5">40</option>
<option value="5">45</option>
<option value="5">50</option>
</select></td></tr>';
echo'<br>';
echo'<input type="submit" name="submit" value="submit">';
echo'</form>';
}
I guess the easiest way for you is the following (a mix of the other solutions and comments):
<?php
echo '<html>';
?>
<body>
<table border=0 cellspacing=0 cellpadding=0>
<?php
$x = 0;
$connect = mysql_connect('localhost', 'root', '');
$db = 'labs';
$tb = 'comments';
$seldb = mysql_select_db($db, $connect);
echo '<form method="POST" action="..'.$_SERVER["PHP_SELF"].'">';
$query = mysql_query('SELECT com_id, comments FROM comments ORDER BY com_id ASC');
while($i = mysql_fetch_assoc($query)) {
$x++;
echo'<tr><td>'.$i['comments'].'</td>';
echo'<td><select name="score_'.$x.'" id="score" size="1">
<option value="5">5</option>
<option value="10">10</option>
<option value="15">15</option>
<option value="20">20</option>
<option value="25">25</option>
<option value="30">30</option>
<option value="35">35</option>
<option value="40">40</option>
<option value="45">45</option>
<option value="50">50</option>
</select></td></tr>';
echo'<br>';
}
echo'<input type="submit" name="submit" value="submit">';
echo'</form>';
if(isset($_POST['submit'])) {
//not sure if all the scores will be stored in here.
$id = $_POST['id'];
for($y = 0;$y <= $x; $y++)
{
//This query is no sql injection save. Please add filters for productive uses!!
$query = mysql_query('UPDATE table_name SET score = '.$_POST["score_".$y].' WHERE id='.$id);
}
?>
</table>
</body>
</html>
Code is no tested!

header(Location) not working [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 9 years ago.
I'm aware of the fact that for header(Location) to work, no output must be sent before. The problem is that I've checked my code so many times but can't find what is actually being sent as output, thus preventing my header(Location) from working.
Can anyone spot the error?
<div id="content">
<h2>Lägg till</h2>
<p>Fyll i fälten och klicka på Lägg till för att skapa en ny kontakt i listan.</p>
<?php
$editid = $_GET['contact_id'];
$query = "SELECT *, Persons.p_id FROM Persons INNER JOIN Pictures ON (Pictures.p_id = Persons.p_id) WHERE Persons.p_id = " . $editid;
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
$name = $row['name'];
$address = $row['address'];
$birthday = $row['birthday'];
$picture = $row['source'];
$p_id = $row['p_id'];
}
?>
<form action="" id="addressForm" method="post">
<ul>
<li><label for="name"><strong>Namn</strong></label><input type="text" name="name" id="name"/></li>
<li><label for="address"><strong>Adress</strong></label><input type="text" name="address" id="address"/></li>
<li><label for="year"><strong>Födelsedag</strong></label>
<select id="year" name="year">
<option value="2013">2013</option>
<option value="2012">2012</option>
<option value="2011">2011</option>
<option value="2010">2010</option>
<option value="2009">2009</option>
<option value="2008">2008</option>
<option value="2007">2007</option>
<option value="2006">2006</option>
<option value="2005">2005</option>
<option value="2004">2004</option>
<option value="2003">2003</option>
<option value="2002">2002</option>
<option value="2001">2001</option>
<option value="2000">2000</option>
<option value="1999">1999</option>
<option value="1998">1998</option>
<option value="1997">1997</option>
<option value="1996">1996</option>
<option value="1995">1995</option>
<option value="1994">1994</option>
<option value="1993">1993</option>
<option value="1992">1992</option>
<option value="1991">1991</option>
<option value="1990">1990</option>
</select>
<select name="month">
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
<option value='4'>4</option>
<option value='5'>5</option>
<option value='6'>6</option>
<option value='7'>7</option>
<option value='8'>8</option>
<option value='9'>9</option>
<option value='10'>10</option>
<option value='11'>11</option>
<option value='12'>12</option>
</select>
<select name="day">
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
<option value='4'>4</option>
<option value='5'>5</option>
<option value='6'>6</option>
<option value='7'>7</option>
<option value='8'>8</option>
<option value='9'>9</option>
<option value='10'>10</option>
<option value='11'>11</option>
<option value='12'>12</option>
<option value='13'>13</option>
<option value='14'>14</option>
<option value='15'>15</option>
<option value='16'>16</option>
<option value='17'>17</option>
<option value='18'>18</option>
<option value='19'>19</option>
<option value='20'>20</option>
<option value='21'>21</option>
<option value='22'>22</option>
<option value='23'>23</option>
<option value='24'>24</option>
<option value='25'>25</option>
<option value='26'>26</option>
<option value='27'>27</option>
<option value='28'>28</option>
<option value='29'>29</option>
<option value='30'>30</option>
<option value='31'>31</option>
</select>
</li>
<li><label for="picture"><strong>Bild (URL)</strong></label><input type="text" name="picture" id="picture"/></li>
<li><input type="submit" id="submit" name="submit" value="Lägg till"/></li>
</ul>
</form>
<?php
if(isset ($_POST['submit']))
{
$editname = mysql_real_escape_string(htmlspecialchars($_POST['name']));
$editaddress = mysql_real_escape_string(htmlspecialchars($_POST['address']));
$editpicture = mysql_real_escape_string(htmlspecialchars($_POST['picture']));
$year = $_POST['year'];
$month = $_POST['month'];
$day = $_POST['day'];
if ($month < 10)
{
$month = "0$month";
}
if ($day < 10)
{
$day = "0$day";
}
$editbirthday = $year . "-" . $month . "-" . $day;
if (!empty($name) && !empty($address)){
$update = "UPDATE Persons SET name = '$editname', address = '$editaddress', birthday = '$editbirthday' WHERE p_id = '$editid'";
$result = mysql_query($update);
$query = "SELECT * FROM Persons WHERE p_id = '$editid' LIMIT 1";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
$p_id = $row['p_id'];
$update = "UPDATE Pictures SET source = '$editpicture' WHERE p_id = '$editid'";
$result = mysql_query($update);
header('Location: index.php');
}
}
}
?>
</div>
Remember that an output could be:
Whitespace before <?php or after ?>
UTF-8 Byte Order Mark
Error messages or notices
print, echo
Raw <html> areas before <?php code.
So you're hitting the final point of this list...
You must put the header ABOVE any HTML output. You can just put it at the top of your document in this case. So just put all the PHP at the top.
Put the whole if statement from the following, to the top of the PHP file. I don't think this will cause any issues.
if(isset ($_POST['submit']))
I hope this helps
You already have output. Headers MUST be set before any output is sent.
"Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP."
Refer to the documentation about it. Hope this helps.
no output
no whitespace
save file without BOM

Categories