Forum Moderators: coopster

Message Too Old, No Replies

new to php, trying to send data from form to a db

         

mgworek

9:14 pm on Feb 16, 2006 (gmt 0)

10+ Year Member



here is my code, basic but its not working.

<?php
$db_name = "database";
$db_login = "username";
$db_password = "password";
$db_host = "localhost";
$db_port = "3306";

$db = mysql_connect($db_host . ":" . $db_port, $db_login, $db_password) or die("Connect error");
mysql_select_db($db_name, $db) or die("Select error");
?>

<html>
<head>
<title>My first MySQL form submission</title>
</head>
<body>
<form action="" method="post">
Company: <input type="text" name="Company"><br>
Phone: <input type="text" name="Phone"><br>
<input type="submit" name="submit" value="Submit!">
</form>

<?php
$Company = $_POST[‘Company’];
$Phone = $_POST[‘Phone’];
mysql_query("INSERT INTO `partner` (Company, Phone) VALUES ('$Company', ‘$Phone’)");
?>
</body>
</html>

any help would be great.

Thank you.

[edited by: jatar_k at 9:30 pm (utc) on Feb. 16, 2006]
[edit reason] generalized login info [/edit]

jatar_k

9:17 pm on Feb 16, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld mgworek,

what exactly isn't working? do you get errors? or do you get nothing?

mgworek

9:24 pm on Feb 16, 2006 (gmt 0)

10+ Year Member



Thank you.

I dont get anything.

I check my database after submiting but nothing is there.

I think it is connecting since I dont get connecton error but like I said, I am new so.

jatar_k

9:39 pm on Feb 16, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



well don't worry about being new, we all are at some point.

a couple of things that strike me

there are a lot of curly quotes in there, not sure why but they might cause some issues. They should be changed to '

do you really need the port? that's just the standard mysql port so it shouldn't be required in the connect, 'localhost' should be enough
ref: [iana.org...]

on to what might help find your problem

I would split this up into different lines

mysql_query("INSERT INTO `partner` (Company, Phone) VALUES ('$Company', ‘$Phone’)");

this is a case of curly quotes and I will change those too

$query = "INSERT INTO partner (Company, Phone) VALUES ('$Company', '$Phone')";
echo '<p>',$query;
mysql_query($query) or die('<p>problem with query: ' . mysql_error());

this does 2 things
1. it echo's your query so you can look at it and make sure there aren't any glaring issues
2. adds the or die so if mysql returns an error from your query you will actually know about it and what it is

I also removed the backticks around the tablename just because I don't use them, not because they are wrong.

give that a try and see what happens

mgworek

9:56 pm on Feb 16, 2006 (gmt 0)

10+ Year Member



thank you.

when I hit submit, I am now getting blank entries inserted into the database.

jatar_k

10:02 pm on Feb 16, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



$Company = $_POST[‘Company’];
$Phone = $_POST[‘Phone’];

it seems the above lines aren't working, furst try just changing the curly quotes like so

$Company = $_POST['Company'];
$Phone = $_POST['Phone'];

next check that 'Company' and 'Phone' are the proper element names from your form, make sure the case is right specifically

mgworek

10:09 pm on Feb 16, 2006 (gmt 0)

10+ Year Member



got it working, i did what u said right before i read your post and it worked!

thank you very much!

jatar_k

10:13 pm on Feb 16, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



glad to help