Jump to content

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

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.