Jump to content

Newb Question regard insert into database


JamesThePanda

Recommended Posts

Hi

Im having trouble with the following bit of code basically I want to ecute the MYSQL query so it inserts the entries in to the database.

 

I assume since Im not doing anything with the $result then the query isnt being excuted.

 

<?php
// form information 
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$phone = $_POST['phone'];
$email = $_POST['email'];
// End of Form information 

// SQL connect
$host = "localhost";
$username = "root";
$password = "";
$db_name= "DB";
$tbl_name= "TABLE";

mysql_connect ( "$host", "$username", "$password" ) or die ("Cant connect to database");
mysql_select_db ("$db_name") or die (mysql_error());
//end sql connect 

// sql query to insert contacts
$sql = "INSERT INTO `jvcom`.`contacts` (`id`, `firstname`, `surname`, `email`, `phone`) VALUES (NULL, $firstname, $lastname, $email, $phone)";

$result = mysql_query($sql) or trigger_error(mysql_error());


?>

 

 

Thanks

 

James

Link to comment
Share on other sites

because you're inserting strings you need to put them in quotes

$sql = "INSERT INTO `jvcom`.`contacts` (`id`, `firstname`, `surname`, `email`, `phone`) VALUES (NULL, '$firstname', '$lastname', '$email', '$phone')";

 

If that is an auto increment id you don't need to insert it either. ie. remove it from the query.

 

hope that works.

Link to comment
Share on other sites

Here is what I have at the moment , I had a few people look at it and it still does work :(

 

 

<?php
// form information 
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$phone = $_POST['phone'];
$email = $_POST['email'];
// End of Form information 

// SQL connect
$host = "localhost";
$username = "root";
$password = "";
$db_name= "db";
$tbl_name= "table";

mysql_connect ( "$host", "$username", "$password" ) or die ("Cant connect to database");
mysql_select_db ("$db_name") or die (mysql_error());
//end sql connect 

// sql query to insert contacts
$sql = "INSERT INTO 'jvcom'.'contacts' ( 'firstname', 'surname', 'email', 'phone') VALUES ( '$firstname', '$lastname', '$email', '$phone')";

mysql_query($sql) or trigger_error(mysql_error());

echo $sql;

// End Of MySQL insert into Database
?>

 

 

Thanks

 

James

Link to comment
Share on other sites

Why on earth did you change the back-ticks you originally had around the table and column names into single-quotes? No one suggested to do that.

 

You are probably not getting any error output from the or trigger_error(....) statement because what it does is dependent on the error_reporting/display_errors/log_errors settings. Either turn on full php error reporting/display errors (you should be developing and debugging php code on a system with error_reporting set to E_ALL and display_errors set to ON anyway to get php to help you) or use the or die(...) statement that bl00dshooter suggested as it will always give you output on an error whereas the or trigger_error() statement won't.

 

 

Link to comment
Share on other sites

Debugging of your sql would be a lot better if you use...

 

trigger_error("SQL: $sql, ERROR: ". mysql_error(), E_USER_ERROR);

...rather than what you already have. You will of course require error_reporting(E_ALL); and display_errors(1);. The reason you are getting problems is that in your first piece of code you were using backticks to surround column names (which is ok) but you were also using them for your string values (which is wrong). On the flip side in your second piece of code you are using single quotes for your string values (which is right) but also on your field names (which is wrong).

 

$sql = "INSERT INTO contacts (firstname, surname, email, phone) VALUES ('$firstname', '$lastname', '$email', '$phone')";

Faux Edit: PFMaBiSmAd replied whilst I was posting there, but I figured since I had included different information I should still post.[/i]

Link to comment
Share on other sites

Hi

 

Here is what I have at the moment. I originally copyied the queries from PhpMyAdmin and that caused a few problem.

I replaced trigger error with die

also i have tried different variation witht he sigle quote double quote and back ticks.

 

I keep getting the same thing happen again and again just a blank screen.

 

no errors at all,  but the values are not appearing in the database

 

 

I realy puzzeled on this one

 

 

Thanks

 

James

 

<?php

error_reporting(E_ALL);

// form information 
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$phone = $_POST['phone'];
$email = $_POST['email'];
// End of Form information 

// SQL connect
$host = "localhost";
$username = "root";
$password = "";
$db_name= "jvcom";
$tbl_name= "contacts";

mysql_connect ( "$host", "$username", "$password" ) or die ("Cant connect to database");
mysql_select_db ("$db_name") or die (mysql_error());
//end sql connect 

// sql query to insert contacts
$sql = "INSERT INTO contacts ('firstname', 'surname', 'email', 'phone') VALUES ( $firstname, $lastname, $email, $phone)";


mysql_query($sql) or die(mysql_error());

echo $sql;

// End Of MySQL insert into Database
?>

Link to comment
Share on other sites

// sql query to insert contacts
$sql = "INSERT INTO contacts ('firstname', 'surname', 'email', 'phone') VALUES ( $firstname, $lastname, $email, $phone)";

 

your query is still wrong. your string values need to be in quotes. not the field names.

 

// sql query to insert contacts
$sql = "INSERT INTO contacts (firstname, surname, email, phone) VALUES ( '$firstname', '$lastname', '$email', '$phone')";

 

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.