NameSilo

PHP code not working

SpaceshipSpaceship
Watch

unixbro-patty

Account Closed
Impact
14
Code:
$connection = mysql_connect("localhost", "$user", "$password") or die("Couldn't connect to database");

$db = mysql_select_db($database, $connection) or die("Couldn't choose database.");

$sql = mysql_query("INSERT INTO $table_name (first_name, last_name, birthdate, address, city, state, zip, phone) VALUES ('$first_name', '$last_name', '$birthdate', '$address', '$city', '$state', '$zip', '$phone')");


can someone tell me why the above code isn't putting anything inat a database except the int and date defaults?

:music:
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
delete the $sql = bit

what your doing is putting that the query function to a variable but not calling the variable afterwards.

what you could do is
PHP:
$sql = "MY MYSQL `query` HERE"
mysql_query($sql);
 
0
•••
We need to see more of the code to try and figure out the problem.
 
0
•••
not sure but if you are getting this info from a form posting with method of POST or GET:

PHP:
$connection = mysql_connect("localhost", "$user", "$password") or die("Couldn't connect to database");

$db = mysql_select_db($database, $connection) or die("Couldn't choose database.");

$sql = mysql_query("INSERT INTO $table_name (first_name, last_name, birthdate, address, city, state, zip, phone) VALUES ('$_POST[first_name]', '$_POST[last_name]', '$_POST[birthdate]', '$_POST[address]', '$_POST[city]', '$_POST[state]', '$_POST[zip]', '$_POST[phone]')");
This will also be a little more secure.
if your using the GET method change _POST to _GET.

Cody Selzer
 
0
•••
you could add this code:
PHP:
if( !$sql ){
echo "Could not add to database";
}

this will run the mysql query AND report on any errors. :) lol

Dunno if this will work though.
 
0
•••
The part you posted looks fine, unixbro. Would have to see the whole script to see what's not working right.
 
0
•••
It sounds like you don't have anything being stored in the variables you trying to insert into the DB. As RJ said, and I said earlier, we need to see more code.
 
0
•••
Code:
<?php
$db_name = "xxxxxxx";
$username = "xxxxxx";
$password = "xxxxxx";
$tbl_name = "xxxxxx";
$connection = mysql_connect("localhost", "$username", "$password") or die("Couldn't connect to database");
$db = mysql_select_db($db_name, $connection) or die("Couldn't choose database.");
$sql = mysql_query("INSERT INTO $tbl_name (first_name, last_name, birthdate, address, city, state, zip, phone) VALUES ('$first_name', '$last_name', '$birthdate', '$address', '$city', '$state', '$zip', '$phone')");
mysql_query($sql);
$result = mysql_query($sql, $connection);
if (!$result) {
   die('Could not query:' . mysql_error());
}
else { 
   die('Data entered correctly.');
}

?>
<html>
<title>Database connect</title>
<body>
<? 
echo "$msg"; 
echo "<p>First Name: $first_name <br>";
echo "<p>Last Name: $last_name <br>";
echo "<p>Birthdate: $birthdate <br>";
echo "<p>Address: $address <br>";
echo "<p>City: $city <br>";
echo "<p>State: $state <br>";
echo "<p>Zip: $zip <br>";
echo "<p>Phone: $phone <br>";
?>
</body>
</html>


There's the whole code still not working
 
0
•••
odd, wheres the form?

PHP:
<?php
$db_name = "xxxxxxx";
$username = "xxxxxx";
$password = "xxxxxx";
$tbl_name = "xxxxxx";
$connection = mysql_connect("localhost", "$username", "$password") or die("Couldn't connect to database");
$db = mysql_select_db($db_name, $connection) or die("Couldn't choose database.");
$sql = @mysql_query("INSERT INTO $tbl_name (first_name, last_name, birthdate, address, city, state, zip, phone) VALUES ('$first_name', '$last_name', '$birthdate', '$address', '$city', '$state', '$zip', '$phone')") OR DIE(mysql_error());



?>
<html>
<title>Database connect</title>
<body>
<? 
echo "$msg"; 
echo "<p>First Name: $first_name <br>";
echo "<p>Last Name: $last_name <br>";
echo "<p>Birthdate: $birthdate <br>";
echo "<p>Address: $address <br>";
echo "<p>City: $city <br>";
echo "<p>State: $state <br>";
echo "<p>Zip: $zip <br>";
echo "<p>Phone: $phone <br>";
?>
</body>
</html>
 
0
•••
axilant ... thanks man it works now. and you helped me learn I appreciate it
 
0
•••
unixbro said:
axilant ... thanks man it works now. and you helped me learn I appreciate it
im pleased to hear you compliment me like that :D

and no problem hit me on any instant messager if you need help (check profile)
 
0
•••
Appraise.net

We're social

Escrow.com
Spaceship
Rexus Domain
CryptoExchange.com
Domain Recover
CatchDoms
DomDB
NameFit
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back