Jump to content

Posting to DB from a form - having trouble - NEWBIE


jduke6

Recommended Posts

Working on a project for school and I am trying to code a form that feeds into a DB. 

 

I have been studying the syntax, reading and doing everything I can to get this to work. 

 

A little help, pointers, direction would be greatly appreciated.

 

It may be my database structure so I included a image for anyone to see...  and the files.

 

Thanks in advance

 

 

 

[attachment deleted by admin]

Link to comment
Share on other sites

I am getting an error saying Notice: Undefined index:

 

I really thought the code I wrote would work no problems but being new I have no real idea how to solve the problem.

 

 


<form action="update.php" method="post">

<p>City:<br/>
<input type="text" name="city" size="30" /></p>

<p>Property Type:<br/>
<select name="type">
<option value="single">Single Family Home</option>
<option value="condo">Condo</option>
<option value="duplex">Duplex</option>
<option value="multi">Multi-Unit</option>
<option value="rental">Rental</option>
</select></p>


<p> Radius:<br/>
<select name="radius">
<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>
</select></p>

<p> Price Range:<br/>
<select name="price">
<option value="under">Under $200,000</option>
<option value="2">$200,000 - $300,000</option>
<option value="3">$300,001 - $400,000</option>
<option value="4">$400,001 - $500,000</option>
<option value="5plus">$500,000 plus</option>
</select></p>


<p> Number of Bedrooms:<br/>
<select name="bedrooms">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5 or more</option>
</select></p>

<p> Number of Bathrooms:<br/>
<select name="bathrooms">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5 or more</option>
</select></p>


<p> Garage Parking Preference:<br />
<input type="checkbox" name="parking" value="yes" /> Yes<br />
<input type="checkbox" name="parking" value="no" /> No
</p>

<p>Please give some details on your ideal property.  This can include features of your ideal home including near an elementary school,<br/> 
parks, Close to Shopping. Feel free to describe anything you would like in the home that has not been covered.<br />
<textarea name="feedback" rows="20" cols="100" wrap="virtual" /></textarea></p>


<p>First name:<br/>
<input type="text" name="firstname" size="40" /></p>

<p>Last name:<br/>
<input type="text" name="lastname" size="40" /></p>
     
<p>email address:<br/>
<input type="text" name="email" size="40" /></p>

<p>Phone Number<br/>
<input type="text" name="phone" size="40" /></p>
     

     
<p><input type="submit" value="Send feedback" /></p>

</form>

 

 

 

 

update.php

<?php 
$city = $_POST['city'];
$radius = $_POST['radius'];
$type = $_POST['type'];
$price = $_POST['price'];
$bedrooms = $_POST['bedrooms'];
$bathrooms = $_POST['bathrooms'];
$parking = $_POST['parking'];
$details = $_POST['details'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$phone = $_POST['phone'];

// 1. Create a database connection
$connection = mysql_connect("localhost", "root", "maven777");
if(!$connection){
	die("Database connection failed: " .mysql_error());
}

// 2. Select a database to use
$db_select = mysql_select_db("homesloans", $connection);
if(!$db_select){
	die("Database selection failed: " .mysql_error());
}

$query="INSERT INTO leads (leadid, city, radius, type, price, bedrooms,
bathrooms, parking, details, firstname, lastname, email, phone)

VALUES('NULL', '[city]', '[radius]', '[type]', '[price]', '[bedrooms]',
		'[bathrooms]', '[parking]', '[details]', '[firstname]', '[lastname]', '[email]', '[phone]')";

mysql_query($query) or die ('Error updating database');

echo "Database Successfully Updated."; 



?>

 

 

Link to comment
Share on other sites

Some things that stand out:

Your form has a textarea named "feedback", but the processing code seems to be looking for a field named "details". This seems to be the cause of the notice you're getting.

Your INSERT query doesn't make use of any of the variables you've defined and to which you've assigned the values from the $_POST array. '[city]' isn't a variable, but '$city' is.

You have no protection from SQL injection; your form data isn't sanitized/validated at all.

the DB field `leadid` should probably be an INT field, set as AUTO_INCREMENT, and used as the table's primary key index.

 

EDIT: take care of the first 2 items and let's see if we can't at least get it inserting data, then start on the other things.

Link to comment
Share on other sites

Fixed - Your form has a textarea named "feedback", but the processing code seems to be looking for a field named "details". This seems to be the cause of the notice you're getting.

Fixed - Your INSERT query doesn't make use of any of the variables you've defined and to which you've assigned the values from the $_POST array. '[city]' isn't a variable, but '$city' is.

Fixed - the DB field `leadid` should probably be an INT field, set as AUTO_INCREMENT, and used as the table's primary key inde

 

 

Still working on this part - I know what it is but how to effectively code is still a mystery..

You have no protection from SQL injection; your form data isn't sanitized/validated at all.

 

Link to comment
Share on other sites

Still working on the sanitizing the input.

 

Also I got the drop down menus to work by changing the the type of data being input to VARCHAR  in the DB

 

db structure link - http://i53.tinypic.com/15qrsk7.jpg

 

<form action="update.php" method="post">

<p>City:<br/>
<input type="text" name="city" size="30" /></p>

<p>Property Type:<br/>
<select name="type">
<option value="Single Family Home">Single Family Home</option>
<option value="Condo">Condo</option>
<option value="Duplex">Duplex</option>
<option value="Multi-Unit">Multi-Unit</option>
<option value="Rental">Rental</option>
</select></p>


<p> Radius:<br/>
<select name="radius">
<option value="5 miles">5</option>
<option value="10 miles">10</option>
<option value="15 miles ">15</option>
<option value="20 miles">20</option>
<option value="25 miles">25</option>
</select></p>


<p> Price Range:<br/>
<select name="price">
<option value="under $200,000">Under $200,000</option>
<option value="$200,000 - $300,000">$200,000 - $300,000</option>
<option value="$300,001 - $400,000">$300,001 - $400,000</option>
<option value="$400,001 - $500,000">$400,001 - $500,000</option>
<option value="Over $500,000">Over $500,000</option>
</select></p>


<p> Number of Bedrooms:<br/>
<select name="bedrooms">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5 or more</option>
</select></p>

<p> Number of Bathrooms:<br/>
<select name="bathrooms">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5 or more</option>
</select></p>


<p> Garage Parking Preference:<br />
<select name="parking">
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
</p><br />  

<p>Please give some details on your ideal property.  This can include features of your ideal home including near an elementary school,<br/> 
parks, Close to Shopping. Feel free to describe anything you would like in the home that has not been covered.<br />
<textarea name="details" rows="20" cols="100" wrap="virtual" /></textarea></p>


<p>First name:<br/>
<input type="text" name="firstname" size="40" /></p>

<p>Last name:<br/>
<input type="text" name="lastname" size="40" /></p>
     
<p>email address:<br/>
<input type="text" name="email" size="40" /></p>

<p>Phone Number<br/>
<input type="text" name="phone" size="40" /></p>
     

     
<p><input type="submit" value="Send feedback" /></p>

</form>





//PHP 

<?php 

error_reporting(E_ALL ^ E_NOTICE); 
?>
<?php 
$city = $_POST['city'];
$radius = $_POST['radius'];
$type = $_POST['type'];
$price = $_POST['price'];
$bedrooms = $_POST['bedrooms'];
$bathrooms = $_POST['bathrooms'];
$parking = $_POST['parking'];
$details = $_POST['details'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$phone = $_POST['phone'];

// 1. Create a database connection
$connection = mysql_connect("localhost", "root", "maven777");
if(!$connection){
	die("Database connection failed: " .mysql_error());
}

// 2. Select a database to use
$db_select = mysql_select_db("homesloans", $connection);
if(!$db_select){
	die("Database selection failed: " .mysql_error());
}

$query="INSERT INTO leads (leadid, city, radius, type, price, bedrooms,
bathrooms, parking, details, firstname, lastname, email, phone)

VALUES('NULL', '[$city]', '[$radius]', '[$type]', '[$price]', '[$bedrooms]',
		'[$bathrooms]', '[$parking]', '[$details]', '[$firstname]', '[$lastname]', '[$email]', '[$phone]')";

mysql_query($query) or die ('Error updating database');

echo "Database Successfully Updated."; 



?>

 

 

 

Link to comment
Share on other sites

How Do I sanitize the input? 

 

FILTER_SANITIZE_EMAIL - for email

 

but what about for regular strings ?

 

also I need to know how to rest the form because when I load it and test it and even though I reload it  the information is still there.

 

 

 

Link to comment
Share on other sites

Tried to implement the function and get this error

 

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\wamp\www\homesloans\update.php on line 48

 

<?php 

error_reporting(E_ALL ^ E_NOTICE); 
?>
<?php 
$city = $_POST['city'];
$radius = $_POST['radius'];
$type = $_POST['type'];
$price = $_POST['price'];
$bedrooms = $_POST['bedrooms'];
$bathrooms = $_POST['bathrooms'];
$parking = $_POST['parking'];
$details = $_POST['details'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$phone = $_POST['phone'];

// 1. Create a database connection
$connection = mysql_connect("localhost", "root", "maven777");
if(!$connection){
	die("Database connection failed: " .mysql_error());
}

// 2. Select a database to use
$db_select = mysql_select_db("homesloans", $connection);
if(!$db_select){
	die("Database selection failed: " .mysql_error());
}

$query="INSERT INTO leads (leadid, city, radius, type, price, bedrooms,
bathrooms, parking, details, firstname, lastname, email, phone),

$city = mysql_real_escape_string($_POST['city']),
$details = mysql_real_escape_string($_POST['details']),
$firstname = mysql_real_escape_string($_POST['firstname ']),
$lastname = mysql_real_escape_string($_POST['lastname']),
$email = mysql_real_escape_string($_POST['email'])
$phone = mysql_real_escape_string($_POST['phone']),



VALUES('NULL', '[$city]', '[$radius]', '[$type]', '[$price]', '[$bedrooms]',
		'[$bathrooms]', '[$parking]', '[$details]', '[$firstname]', '[$lastname]', '[$email]', '[$phone]')";




mysql_query($query) or die ('Error updating database');

echo "Database Successfully Updated."; 



?>

Link to comment
Share on other sites

You've tried to add the values into the query string, but not quite done it properly. I would do it in this order for the moment. There are other things that still need to be taken care of, but one step at a time, yes?

 

<?php
if( strtolower($_SERVER['REQUEST_METHOD']) === 'post' ) { // make sure the form has actually been submitted . . .
// 1. Create a database connection
$connection = mysql_connect("localhost", "root", "maven777");
if(!$connection){
	die("Database connection failed: " .mysql_error());
}

// 2. Select a database to use
$db_select = mysql_select_db("homesloans", $connection);
if(!$db_select){
	die("Database selection failed: " .mysql_error());
}
$radius = $_POST['radius'];
$type = $_POST['type'];
$price = $_POST['price'];
$bedrooms = $_POST['bedrooms'];
$bathrooms = $_POST['bathrooms'];
$parking = $_POST['parking'];
$city = mysql_real_escape_string($_POST['city']);
$details = mysql_real_escape_string($_POST['details']);
$firstname = mysql_real_escape_string($_POST['firstname ']);
$lastname = mysql_real_escape_string($_POST['lastname']);
$email = mysql_real_escape_string($_POST['email']);
$phone = mysql_real_escape_string($_POST['phone']);

$query="INSERT INTO leads (leadid, city, radius, type, price, bedrooms,
bathrooms, parking, details, firstname, lastname, email, phone),
VALUES('NULL', '$city', '$radius', '$type', '$price', '$bedrooms',
'$bathrooms', '$parking', '$details', '$firstname', '$lastname', '$email', '$phone')";

mysql_query($query) or die ('Error updating database');

echo "Database Successfully Updated."; // not necessarily. The only way to know for sure is to check  that mysql_affected_rows() > 0
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.