Jump to content

[SOLVED] Correct syntax


genesysmedia

Recommended Posts

Can someone please show me the correct syntax for inserting data into a mysql database using superglobals?

 

Thank you in advance.

 

<?php

 

$dbh = mysql_connect("localhost", "{db_user}", "{db_password}") or die ('I cannot connect to the database.');

mysql_select_db("{db_name}");

 

$insert_data = "INSERT INTO jobs set id='',

business_name='$_POST['business_name']'";

 

mysql_query($insert_data, $dbh);

 

?>

Link to comment
https://forums.phpfreaks.com/topic/165880-solved-correct-syntax/
Share on other sites

This is how I do it ;

 

// in a config file

 

$HOST = 'YOURHOST';

$USER = "YOURUSERNAME";

$PASS = "YOURPASSWORD";

$DATA = "DATABASENAME";

 

function connect_data()

{

    global $HOST, $USER, $PASS, $DATA;

    mysql_connect($HOST, $USER, $PASS) or die('Connection Failed');

    mysql_select_db($DATA);

}

 

// call the connect function

 

require_once('Location of your config.php file ');connect_data();

 

$insert_data= "INSERT INTO jobs

(

id,business_name

)VALUES(

'','$_POST[business_name]'

);

 

Try that

 

NOTE SET= is used in Update not insert

 

Sorry my mistake

 

I dont use this part (but it is good for complex statements so you can display them on the screen if there is a problem)

$insert_data = "INSERT INTO jobs (id,business_name)VALUES('','$_POST[business_name]'";

 

I put it straight into the mysql_query()

 

mysql_query("INSERT INTO jobs (id,business_name)VALUES('','$_POST[business_name]'") or die ('you can put your own notation comments here'.mysql_error());

 

Sorry... i forgot to put the mysql_query() but it in earlier :(

 

Considering that haku (guru) says that you should use mysql_query("$YOURQUERY","$YOURDBH"); perhaps this is better.

 

Cheers

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.