/    Sign up×
Community /Pin to ProfileBookmark

Using the INSERT INTO function with php

I haven’t used PHP for a while I have set up a form but I am struggling inputting this data to MySQL.

Here is my PHP code:

[code=php]<?php
require ‘connect.inc.php’;
$con = (mysql_connect($mysql_host, $mysql_user, $mysql_pass));

mysql_connect($mysql_host, $mysql_user, $mysql_pass);
mysql_select_db($mysql_db);

$sql=”INSERT INTO ‘1’ VALUES ($_POST[fullname],$_POST[grade],$_POST[joindate])”;
if (!mysql_query($sql,$con))
{
die(‘Error: ‘ . mysql_error());
}
echo “1 record added”;

mysql_close($con)
?>[/code]

The part I’m looking for is mainly this line:

[code=php]$sql=”INSERT INTO ‘1’ VALUES ($_POST[fullname],$_POST[grade],$_POST[joindate])”;[/code]

As i Say i have not used PHP for a long time and the problem is probably a new update or a simple thing i have forgot.

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@WolfShadeMar 20.2012 — You named the database table '1'?

It's been a while since I've used MySQL. What about:
[CODE]$sql="INSERT INTO [COLOR="Red"][/COLOR]1[COLOR="Red"][/COLOR] VALUES ([COLOR="Red"]'[/COLOR]$_POST[fullname][COLOR="Red"]'[/COLOR],[COLOR="Red"]'[/COLOR]$_POST[grade][COLOR="Red"]'[/COLOR],[COLOR="Red"]'[/COLOR]$_POST[joindate][COLOR="Red"]'[/COLOR])"; [/CODE]
Copy linkTweet thisAlerts:
@NogDogMar 20.2012 — It's a good practice to include a column list in your insert query, even though it's not always required -- but it is if you are not providing a value for every column or not providing the values in the order the DB expects them.
<i>
</i>INSERT INTO <span><code>table_name</code></span> (<span><code>col_1</code></span>, <span><code>col_2</code></span>, <span><code>col_3</code></span>) VALUES ('val_1', 'val_2', 'val_3')
Copy linkTweet thisAlerts:
@mitch_bauthorMar 20.2012 — Thank you NogDog and I only named the table '1' for testing purposes.
Copy linkTweet thisAlerts:
@mitch_bauthorMar 20.2012 — Ok I Took your sergesstion and that error seems to be fixed now but I now have another.

[CODE]Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ',)' at line 1[/CODE]


[code=php]
<?php
require 'connect.inc.php';
$con = (mysql_connect($mysql_host, $mysql_user, $mysql_pass));

mysql_connect($mysql_host, $mysql_user, $mysql_pass);
mysql_select_db($mysql_db);


$sql="INSERT INTO 1 (fullname, grade, joindate) VALUES ($_POST[fullname],$_POST[grade],$_POST[joindate])";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";

mysql_close($con)

?>
[/code]
Copy linkTweet thisAlerts:
@WolfShadeMar 20.2012 — That means that one of the values being submit to the database is blank or null.

Instead of [B]VALUES ($_POST[fullname],$_POST[grade],$_POST[joindate])[/B], it's getting [B]VALUES ($_POST[fullname],$_POST[grade][COLOR="Red"],)[/COLOR][/B]
Copy linkTweet thisAlerts:
@mitch_bauthorMar 21.2012 — Ahh ok but I get this before the chance to enter the dater in the form appears how do I fix this or do I have to put a default value?
Copy linkTweet thisAlerts:
@GalwayMar 21.2012 — Ahh ok but I get this before the chance to enter the dater in the form appears how do I fix this or do I have to put a default value?[/QUOTE]

Not sure of your situation, but by general rule of thumb you'll only want to be inserting things into your database after submitting a form. So do a check to make sure the form has actually been submitted.

<i>
</i>&lt;?php
if(isset($_POST['submit']))//or whatever your submit button's name is (assuming you use one)
{
//run sql statements here
}
?&gt;
×

Success!

Help @mitch_b spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...