undefined index after submitting form - php

<?php
include 'includes/connectie.php';
$product_id=$_GET['id'];
$sql = "SELECT * FROM `producten` WHERE product_id='$product_id'";
$sql_result = $dbh->query($sql);
foreach($sql_result as $row)
{
$prijs=$row['prijs'];
$product_naam=$row['product_naam'];
$product_categorie=$row['product_categorie'];
$product_specificaties=$row['product_specificaties'];
$foto=$row['foto'];
$product_id=$row['product_id'];
$product_soort=$row['product_soort'];
echo "Product id nummer:", $product_id;
}
//$_SESSION['prijs'] = $prijs;
if ($_SERVER["REQUEST_METHOD"] == "POST"){
if (!empty($product_naam) && !empty($product_specifcaties) && !empty($product_categorie) &&
!empty($prijs)
&& !empty($product_soort)) {
print "Product aangepast!";
$sql = "UPDATE producten
SET prijs='$prijs', product_naam='$product_naam',
product_specificaties='$product_specificaties',
product_categorie='$product_categorie', product_soort='$product_soort'
WHERE product_id='$product_id'";
$query = $db->prepare( $sql );
$result = $query->execute();
exit();
}
}
?>
<html>
<form name="admin" action="producten_echt_aanpassen.php" method="POST" id="adminform" enctype="multipart/form-data">
<p>
<label for 'product_naam'>Naam: </label><br>
<input type="text" name="product_naam" value="<?php print $product_naam; ?>"/>
</p>
<p> <label for 'product_specificaties'>Specificaties: </label><br>
<textarea rows= "4" cols="50" name="product_specificaties"><?php print $product_specificaties; ?>
</textarea>
</p>
<p>
<label for 'prijs'>Prijs: </label><br>
<input type="text" name="prijs" value="<?php print $prijs; ?>"/>
</p>
<p>
<label for 'product_categorie'>Iphone: </label><br>
<input type="text" name="product_categorie" value="<?php print $product_categorie; ?>"/>
</p>
<p>
<label for 'product_soort'>Soort: </label><br>
<input type="text" name="product_soort" value="<?php print $product_soort; ?>"/>
</p>
<br/>
<label for 'uploadfile'>Kies foto <img src="<?php print $foto; ?>"></label><br>
<input type="file" name="file" ><br><br>
<input type="submit" name="submit" value="Submit">
</form>
</html>
the variable is loaded in a form in which product details can be changed. the form links to this page with the code above. but whenever I submit the form and try to change te detail i get an error of an undefined index. which is what the $_GET does in line 5. The foreach loop needs the index to be defined but whenever the form is submitted, the index in the URL is gone so the loop doesnt produce the variables that need to go to the database. I hope this makes sense. Can anybody help me out please?

Your html does not include a field named id.
You are sending the form as POST not GET, so after you add the correct field in the HTML you need to refer to it as $product_id = $_POST['id'];
No need for enctype="multipart/form-data", it will only be helpful when you are uploading files. Otherwise it can cause you problems.
<p>
<label for 'product_naam'>Naam: </label><br>
<input type="text" name="product_naam" value="<?php print $product_naam; ?>"/>
</p>
<p> <label for 'product_specificaties'>Specificaties: </label><br>
<textarea rows= "4" cols="50" name="product_specificaties"><?php print $product_specificaties; ?>
</textarea>
</p>
<p>
<label for 'prijs'>Prijs: </label><br>
<input type="text" name="prijs" value="<?php print $prijs; ?>"/>
</p>
<p>
<label for 'product_id'>Product ID: </label><br>
<input type="text" name="id" value="<?php print $product_id; ?>"/>
</p>
<p>
<label for 'product_categorie'>Iphone: </label><br>
<input type="text" name="product_categorie" value="<?php print $product_categorie; ?>"/>
</p>
<p>
<label for 'product_soort'>Soort: </label><br>
<input type="text" name="product_soort" value="<?php print $product_soort; ?>"/>
</p>
<br/>
<label for 'uploadfile'>Kies foto <img src="<?php print $foto; ?>"></label><br>
<input type="file" name="file" ><br><br>
<input type="submit" name="submit" value="Submit">
Hope this helps!

Related

Filling html forms with mysql data using php coming up null

I am trying to fill a html form with data being received out of my mysql database. However I cannot set the forms to display on-load the variables being extracted from the database. I would like the form on-load to hold the data last entered into the forms which have been added to the database previously.
$query = "SELECT FROM character_tbl WHERE character_player
='".$_SESSION["user"]."' character_tbl";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
$name = $row['character_name'];
$race = $row['character_race'];
$class = $row['character_class'];
$alignment = $row['character_alignment'];
$hp = $row['character_hp'];
$str = $row['character_str'];
$dex = $row['character_dex'];
$con = $row['character_con'];
$int = $row['character_int'];
$wis = $row['character_wis'];
$cha = $row['character_cha'];
$ac = $row['character_ac'];
$touch = $row['character_touch'];
$flat = $row['character_flat'];
$fort = $row['character_fort'];
$ref = $row['character_ref'];
$will = $row['character_will'];
}
echo $will;
mysql_close();
?>
<!DOCTYPE html>
<html>
<body>
<div id="nav">
<form action="user.php">
<input type="submit" value="Back">
</form>
</div>
<div id="section">
<form action="update.php" method="POST">
Character Name:<br>
<input type="text" name="name" value="<?php echo $name;?>">
<br>
Race<br>
<input type="text" name="race" value="<?php echo $race;?>">
<br>
Class<br>
<input type="text" name="class" value="<?php echo $class;?>">
<br>
Alignment<br>
<input type="text" name="alignment" value="<?php echo $alignment;?>">
<br>
HP<br>
<input type="text" name="hp" value="<?php echo $hp;?>">
<br>
STR<br>
<input type="number" name="str" value="<?php echo $str;?>">
<br>
DEX<br>
<input type="number" name="dex" value="<?php echo $dex;?>">
<br>
CON<br>
<input type="text" name="con" value="<?php echo $con;?>">
<br>
INT<br>
<input type="text" name="int" value="<?php echo $int;?>">
<br>
WIS<br>
<input type="text" name="wis" value="<?php echo $wis;?>">
<br>
CHA<br>
<input type="text" name="cha" value="<?php echo $cha;?>">
<br>
AC<br>
<input type="text" name="ac" value="<?php echo $ac;?>">
<br>
Touch AC<br>
<input type="text" name="touch" value="<?php echo $touch;?>">
<br>
Flat-Footed AC<br>
<input type="text" name="flat" value="<?php echo $flat;?>">
<br>
Fortitude<br>
<input type="text" name="fort" value="<?php echo $fort;?>">
<br>
Reflex<br>
<input type="text" name="ref" value="<?php echo $ref;?>">
<br>
Will<br>
<input type="text" name="will" value="<?php echo $will;?>">
</br>
<input type="submit" value="Update">
</form>
I think the SQL has error:
SELECT FROM character_tbl WHERE character_player
try:
SELECT * FROM character_tbl WHERE character_player
You have syntax error in your mysql query. You have not place field or columns name or (*) for all columns to extract.
Try like this..
$query = "SELECT * FROM character_tbl WHERE character_player ='".$_SESSION['user']."'";

save fields values during form submit

I want that when the user submits the form, and some reason happen some error, I want that the fields that the user already wrote to be saved, so I want to be show the values the user already wrote.
Im doing this with code below, but I dont understand why, when I submit the form the fields stay empty.
Somebody there see something wrong?
<?php
if (isset($_POST['sendForm'])) {
$f['name'] = $_POST['name'];
$f['content'] = $_POST['content'];
$f['date'] = $_POST['date'];
} ?>
<form name="form" action="" method="post">
<label class="line">
<span class="data">Name:</span>
<input type="text" name="name" value="<?php if (isset($f['name'])) echo $f['name']; ?>"/>
</label>
<label class="line">
<span class="data">Content:</span>
<textarea name="content" rows="3" value="<?php if (isset($f['content'])) echo $f['content']; ?>"></textarea>
</label>
<label class="line">
<span class="data">Date:</span>
<input type="text" name="date" value="<?php if (isset($f['date'])) {
echo $f['date'];
} else {
echo date('d/m/Y H:i:s');
} ?>"/>
</label>
<input type="submit" value="Create" name="sendForm" class="btn"/>
</form>
In short you can set by this way,
<input type="text" id="name" name="name" value="<?php if (isset($_POST['name'])) echo $_POST['name']; ?>" />
You do not need to use $f['name']. you can get value directly by $_POST['name'] method.

trying to show the email and password Written in email and password inputs issue

I´m trying to show in a '<pre></pre>' the values that are passed in the form.
But Its not working and give my some errors:
-> Notice: Undefined index: email in
-> Notice: Undefined index: pass
-> Notice: Undefined index: remember
I already tried put isset like this:
$f['email'] = mysql_real_escape_string(isset($_POST['email']));
$f['pass'] = mysql_real_escape_string(isset($_POST['pass']));
$f['save'] = mysql_real_escape_string(isset($_POST['remember']));
And this way above I dont have erros but the data that I write in the form inputs dont show in my
echo '<pre class="debug">';
print_r($f);
echo '</pre>';
Can you see something that Im doing wrong?
My code:
<?php
if(isset($_POST['sendLogin']))
{
$f['email'] = mysql_real_escape_string($_POST['email']);
$f['pass'] = mysql_real_escape_string($_POST['pass']);
$f['save'] = mysql_real_escape_string($_POST['remember']);
echo '<pre class="debug">';
print_r($f);
echo '</pre>';
}
?>
<?php
if(!isset($_GET['remember']))
{
?>
<form name="login" action="" method="post">
<label>
<span>Email:</span>
<input type="text" class="radius" name="<?php if($f['email']) echo $f['email']; ?>" />
</label>
<label>
<span>Password:</span>
<input type="password" class="radius" name="<?php if($f['pass']) echo $f['pass']; ?>" />
</label>
<input type="submit" value="Login" name="sendLogin" class="btn" />
<div class="remember">
<input type="checkbox" name="remember" value="1"
<?php if(isset($f['save'])) echo 'checked="checked"' ?>
/>
I think that you have made mistake with name attribute. You should have static name="email" and name="pass" in the inputs. While your current name attribute change to value:
<input type="text" class="radius" name="email" value="<?php if($f['email']) echo $f['email']; ?>" />
<input type="password" class="radius" name="pass" value="<?php if($f['pass']) echo $f['pass']; ?>" />
Your inputs must have the name attribute in order to be accessible in the PHP
<form name="login" action="" method="post">
<label>
<span>Email:</span>
<input type="text" class="radius" value="<?php if($f['email']) echo $f['email']; ?>" name="email" />
</label>
<label>
<span>Password:</span>
<input type="password" class="radius" value="<?php if($f['pass']) echo $f['pass']; ?>" name="password" />
</label>
<input type="submit" value="Login" name="sendLogin" class="btn" />
<div class="remember">
<input type="checkbox" name="remember" value="1"
<?php if(isset($f['save'])) echo 'checked="checked"' ?>
/>
In <input type="text"> as well as in <input type="password"> block, provide a meaningful value for the name attribute.
Here in your code provide name="email" and for the next one name="pass".
Also to display the email/pass values after the form submission, give it in value attribute.
Right now you wrongly given inside the name attribute.
value="<?php if($f['email']) echo $f['email']; ?>" and
value="<?php if($f['pass']) echo $f['pass']; ?>"

How to get form fields refilled when data is incorrect

The script for handling the action of my form redirects to the form page if the values are not in proper format. I want to fill the textfields and textarea with the faulty data the user entered on redirect to the form page. I have written the following script which redirects the page on wrong value submission, but does not fill the fields thereafter.
script on form page:
<?php
if(session_id('stat')=="true")
{
$isbn=$_SESSION['var1'] ;
$name=$_SESSION['var2'] ;
$author=$_SESSION['var3'] ;
$publisher=$_SESSION['var4'];
$price=$_SESSION['var5'];
$descrip=$_SESSION['var6'];
$status=$_SESSION['stat'];
}
else
{
$isbn="";
$name="";
$author="";
$publisher="";
$price="";
$descrip="";
$status=false;
}
?>
The html part of the form:
<form action="scripts/addscript.php" method="POST" enctype="multipart/form-data" name="form1" id="form1">
<label for="isbn">ISBN</label>
<input type="text" name="isbn" id="isbn" value="<?php $isbn ?>"/>
<p>
<label for="name">Name</label>
<input type="text" name="name" id="name" value="<?php echo $name; ?>"/>
</p>
<p>
<label for="author">Author</label>
<input type="text" name="author" id="author" value="<?php echo $author; ?>"/>
</p>
<p>
<label for="publisher">Publisher</label>
<input type="text" name="publisher" id="publisher" value="<?php echo $publisher; ?>"/>
</p>
<p>
<label for="price">Price</label>
<input type="text" name="price" id="price" value="<?php echo $price;?>"/>
</p>
<p>
<label for="description">Description</label>
<textarea name="description" id="description" cols="45" rows="5"><?php echo $descrip; ?></textarea>
</p>
<p>
<label for="img">Select an image for the book:
<input type="file" name="img" id="img" />
</label>
<input type="submit" name="submit" id="submit" value="Submit"/>
</p>
</form>
The redirecting script on addscript.php to which the form values are submitted:
<?php
// Get values from form
$isbn=$_POST['isbn'];
$name=$_POST['name'];
$author=$_POST['author'];
$publisher=$_POST['publisher'];
$price=$_POST['price'];
$descrip=$_POST['description'];
$_SESSION['var1'] = $isbn;
$_SESSION['var2'] = $name;
$_SESSION['var3'] = $author;
$_SESSION['var4'] = $publisher;
$_SESSION['var5'] = $price;
$_SESSION['var6'] = $descrip;
if(strlen($isbn)==0||strlen($name)==0||strlen($author)==0||strlen($publisher)==0||strlen($price)==0)
{
$_SESSION['stat']="true";
header('Location: ' . $_SERVER['HTTP_REFERER']);
}
Please telll me where is the problem and how can I solve the issue?
Thanks in advance.
session_start() must be called at the top of your PHP script to use the $_SESSION variable.

PHP comment form

I have a comment form that I have created. It gets the id from the database and prints out the data that goes with that id, but it also prints out the information into the form. How can I get a blank form, so that the user can add a comment to the record?
This the code for the form:
<form method="post" action="pv.php?id=<?php echo $row['ID']?>&action=<?php echo $form_action ?>">
<fieldset>
<legend></legend>
<p>
<label for="cname">Date Of Birth</label> *
<input id="cname" name="dateofbirth" class="required date" value="<?php echo $row['Date_Of_Birth']?>" /> (eg 1978.11.11)
</p>
<p>
<label for="cgender">Gender</label> *
<input type="radio"
name="gender"
value="Male"
<?php if($row['Gender']=='male'){echo 'checked';}?>/>
Male
<input type="radio"
name="gender"
value="Female"
<?php if($row['Gender']=='female'){echo 'checked';}?>/> Female </td>
</p>
<p>
<label for="curl">Title</label> *
<select name="title" id="title" class="required">
<option value="">Please Select</option>
<option value="Mr" <?php if($row['Title']=='Mr'){echo 'selected';}?>>Mr</option>
<option value="Ms" <?php if($row['Title']=='Ms'){echo 'selected';}?>>Ms</option>
<option value="Mrs" <?php if($row['Title']=='Mrs'){echo 'selected';}?>>Mrs</option>
<option value="Miss" <?php if($row['Title']=='Miss'){echo 'selected';}?>>Miss</option>
<option value="Other" <?php if($row['Title']=='Other'){echo 'selected';}?>>Other</option>
</select>
</p>
<p>
<label for="ccomment">First Name</label> *
<input type="text" name="firstname" value="<?php echo $row['First_Name']?>" maxlength="50" />
</p>
<p>
<label for="cemail">Last Name</label> *
<input id="cemail" type="text" name="lastname"
value="<?php echo $row['Last_Name']?>" maxlength="75" />
</p>
<p>
<label for="ccomment">Address 1</label>*
<input type="text" name="address1"
value="<?php echo $row['Address_Line_1']?>" maxlength="50" />
</p>
<p>
<label for="ccomment">Address 2</label>
<input type="text" name="address2"
value="<?php echo $row['Address_Line_2']?>" maxlength="50" />
</p>
<p>
<label for="ccomment">City</label>*
<input type="text" name="city"
value="<?php echo $row['City']?>" maxlength="50" />
</p>
<p>
<label for="ccomment">Postcode</label>*
<input type="text" name="postcode"
value="<?php echo $row['Postcode']?>" maxlength= "10" /> (eg LE5 5QE)
</p>
<p>
<label for="ccomment">Contact No</label>*
<input type="text" name="contactno"
value="<?php echo $row['Contact_No']?>" maxlength= "12" /> (eg 077448825723)
</p>
<p>
<label for="ccomment">Email</label>*
<input type="text" name="email"
value="<?php echo $row['Email']?>" maxlength= "40"/> (eg info#example.com)
</p>
<p>
<label for="ccomment">Comment</label>
<textarea rows="10" cols="30" name="note"
maxlength= "500"><?php echo $row['Additional_Comment']?></textarea>
</p>
<p>
<input class="submit" type="submit" value="Submit"/>
</p>
<p>
<a href='pv.php'>Main Page</a>
</p>
</fieldset>
</form>
This is the code for printing out the data on the page:
if($_GET['action'] == 'comment') {
$form_action = 'comment_ok';
$id = $_GET['id'];
$result = mysql_query("SELECT * FROM project_data WHERE id='$id'");
$row = mysql_fetch_array($result);
echo'<b>';
echo $row['Date_Of_Birth'];
echo '&nbsp&nbsp';
echo $row['Gender'];
echo '&nbsp&nbsp';
echo $row['Title'];
echo '&nbsp&nbsp';
echo $row['First_Name'];
echo '&nbsp&nbsp';
echo $row['Last_Name'];
echo '&nbsp&nbsp';
echo $row['Address_Line_1'];
echo '&nbsp&nbsp';
echo $row['Address_Line_2'];
echo '&nbsp&nbsp';
echo $row['City'];
echo '&nbsp&nbsp';
echo $row['Postcode'];
echo '&nbsp&nbsp';
echo $row['Contact_No'];
echo '&nbsp&nbsp';
echo $row['Email'];
echo '&nbsp&nbsp';
echo $row['Additional_Comment'];
echo '</b>';
}
And a snippet of the code I am using to send the id to the form:
echo "<td><a href='pv.php?action=edit&id=" . $row['ID'] .
"'>Edit</a>&nbsp&nbsp<a href='pv.php?action=delete_ok&id=" . $row['ID'] .
"'>Delete</a>&nbsp&nbsp**<a href='pv.php?action=comment&id=" . $row['ID'] .
"'>Comment</a></td>"**;
echo "</tr>";
How can I do it?
If you want one page that allows the user to fill in a blank form, see a filled out form and update a form.
You can do the blank form or filled form by checking if an id exist, like this:
<?
if (isset(id)) // Here you can check if the id exists, which means you will be doing an update to the SQL query
echo'
<form method="post" action="pv.php?id=' . $row['ID'] . '&action=' . $form_action . '">
<!-- Your form here with values -->
e.g. <label for="cemail">Last Name</label> *
<input id="cemail" type="text" name="lastname" value="' . $row['Last_Name'] . '" maxlength="75" />
</form>';
// Put your SQL update here to take the values from the above form
}
elseif (!isset(id)) // Here there is no id so you will want to insert
{
echo'
<form method="post" action="pv.php?id=' . $row['ID'] . '&action=' . $form_action . '">
<!-- Your form here with values -->
e.g. <label for="cemail">Last Name</label> *
<input id="cemail" type="text" name="lastname" maxlength="75" /> // No value here because will insert new record
</form>';
// Put your SQL insert here to create a new record
}
?>
If you may want the user to be able to add a new comment entirely rather than editing the one already associated with their id, I think you would have to do this by giving them the option to add another comment. To do this, I would create a new table and then insert the comments into that, so you have one table, example_users with the user information, and then another table, user_comments, with the user comments, for example:
Table example_users
id = 1
fname=Joe
lname=Bloggs
email=jbloggs#example.com
Table user_comments
id=1
user_id=1
comment=Comment will be saved here
This way, any user can have any number of comments. You could render this on the page by using a foreach statement to render a text box with all their existing comments and then have a blank one at the end for any new ones. Then they can edit any comment and add a new comment.
If you want the form to be blank, then just set the value attribute to empty. E.g value=''. Except for the id field.

Categories