Forum Moderators: coopster

Message Too Old, No Replies

simple INSERT problem

         

generic

2:05 am on Jul 28, 2009 (gmt 0)

10+ Year Member



Hi all,

I'm sure someone here will spot this immediately I'm working on a (very) basic little script to add customers information to a database and being a visual designer moreso than a programmer, I can't see where I'm going wrong with this. It won't insert the info when I submit.

Any help would be appreciated, thanks!

gen

<?php
function createform() {
echo '<form action="'. $_SERVER['REQUEST_URI'] .'" method="post">
<p>Name:<br />
<input type="text" name="name" id="name" />
</p>
<p>Email:<br />
<input type="text" name="email" id="email" />
</p>
<p>Phone:<br />
<input type="text" name="phone" id="phone" />
</p>
<p>Address:<br />
<textarea name="address" id="address" cols="45" rows="5"></textarea>
</p>
<p>Comments: <br />
<textarea name="comments" id="comments" cols="45" rows="5"></textarea>
</p>
<p>
<input type="submit" name="submit" id="submit" value="Submit" />
<input type="reset" name="reset" id="reset" value="Reset" />
</p>
</form>';
}

$submit = $_GET['submit'];

if(isset($submit)) {

$name = $_GET['name'];
$email = $_GET['email'];
$phone = $_GET['phone'];
$address = $_GET['address'];
$comments = $_GET['comments'];

mysql_query("INSERT INTO customers (name, email, phone, address, details) VALUES('$name', '$email', '$phone', '$address', '$comments') ") or die(mysql_error());

echo '<p class="success">Customer Added!</p>
<ul>
<li>Name: '. $name .'</li>
<li>Email: '. $email .'</li>
<li>Phone: '. $phone .'</li>
<li>Address: '. $address .'</li>
<li>Comments: '. $comments .'</li>
</ul>';

} else {

createform();

}

?>

dreamcatcher

6:43 am on Jul 28, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your data is $_POST data, but you are accessing it as $_GET, which is for query strings. Your code should read:

if(isset($_POST['submit'])) {

$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$address = $_POST['address'];
$comments = $_POST['comments'];

dc

Tommybs

6:44 am on Jul 28, 2009 (gmt 0)

10+ Year Member



I think you should be looking for $_POST['submit'] not $_GET as your form action is POST. It's the same for all your $name variables etc.

Try changing all $_GET to $_POST

generic

4:22 pm on Jul 28, 2009 (gmt 0)

10+ Year Member



Okay, updated it but still no luck...

Tommybs

7:43 pm on Jul 28, 2009 (gmt 0)

10+ Year Member



what output are you getting now? Does it just display the form?

Pico_Train

8:05 pm on Jul 28, 2009 (gmt 0)

10+ Year Member



Try this:

("INSERT INTO customers (name, email, phone, address, details) VALUES("'.$name.'", "'.$email.'", "'.$phone.'", "'.$address.'", "'.$comments.'")

assuming those fields are all text type fields.

generic

8:13 pm on Jul 28, 2009 (gmt 0)

10+ Year Member



@ Tommy B, yea, actually it dumps me right back out to the main "customers.php" page. This is an included file ("new_customer.php") that is called when I reference customers.php?a=addnew. And before anyone asks, I've already tried putting the included code right into the main page.

@Pic, I'll try it once I get finished work for the day and let you know how it goes.

Does it have anything to do with this part:

mysql_query("INSERT INTO customers (name, email, phone, address, details) VALUES('$name', '$email', '$phone', '$address', '$comments') ") or die(mysql_error());

Thanks again folks, if it were a CSS problem I'd be good to go but I'm lost when it comes to PHP...

Tommybs

8:22 pm on Jul 28, 2009 (gmt 0)

10+ Year Member



try setting the "action" directly to a specific page name and handling the submission that way just in case there are issues with

form action="'. $_SERVER['REQUEST_URI'] .'"

Just something else to check

Pico_Train

5:43 am on Jul 29, 2009 (gmt 0)

10+ Year Member



Yes change this

mysql_query("INSERT INTO customers (name, email, phone, address, details) VALUES('$name', '$email', '$phone', '$address', '$comments') ") or die(mysql_error());

to

mysql_query("INSERT INTO customers (name, email, phone, address, details) VALUES("'.$name.'", "'.$email.'", "'.$phone.'", "'.$address.'", "'.$comments.'") ") or die(mysql_error());

and if that doesn't work, try this in case too:

mysql_query("INSERT INTO customers (name, email, phone, address, details) VALUES('".$name."', '".$email."', '".$phone."', '".$address."', '".$comments."') ") or die(mysql_error());

Pico_Train

5:47 am on Jul 29, 2009 (gmt 0)

10+ Year Member



Also have you changed $submit = $_GET['submit'];

to

$submit = $_POST['submit'];

Post your code as you have it now too please...