Jump to content

Trying to insert data into a MySQL database


mikhl

Recommended Posts

Hi,

 

I'm having trouble adding data to the database (test) that i created. I just can't see where I'm going wrong. need some other peoples opinions.

 

I have tried taking the id out of the SQL statement completely as it is an auto increment, but that doesn't seem to work either.

 

Thanks in advance...

 

<?php

$fname = $_POST['fname'];
$sname = $_POST['sname'];
$email = $_POST['email'];
$pword = $_POST['password'];
$gender = $_POST['gender'];
   
$user_name = "root";
$password = "";
$database = "test";
$server = "127.0.0.1";

$db_handle = mysql_connect($server, $user_name, $password);

$db_found = mysql_select_db($database, $db_handle);

if ($db_found)
{
	$SQL = "INSERT INTO members (id, fname, sname, email, password, gender) VALUES (NULL, $fname, $sname, $email, $pword, $gender);";
	$result = mysql_query($SQL);

	mysql_close($db_handle);

	print "Thanks for joining us ".$fname.".";
	print "<br /><br />";
	print $fname . "<br />";
	print $sname . "<br />";
	print $email . "<br />";
	print $pword . "<br />";
	print $gender . "<br />";
}
else
{
	print "Database NOT Found";
	mysql_close($db_handle);
}

?>

Link to comment
Share on other sites

How are you having trouble? Are you receiving any errors?

 

One thing, don't use the id in the query if it is auto-incremented. And, server is usually 'localhost' not your local ip address. Can you connect to the database? Have you got anything similar working? We need to know exactly what you see - turn on error reporting to full.

 

<?php error_reporting(-1); ?>

 

and see if any errors come up.

Link to comment
Share on other sites

How are you having trouble? Are you receiving any errors?

 

One thing, don't use the id in the query if it is auto-incremented. And, server is usually 'localhost' not your local ip address. Can you connect to the database? Have you got anything similar working? We need to know exactly what you see - turn on error reporting to full.

 

<?php error_reporting(-1); ?>

 

and see if any errors come up.

 

Done the error reporting and made no difference.

 

I can connect to the database and have a script that displays all the contents of the members table (which is the table that i am trying to enter data into), I entered this data through PHP My Admin GUI.

 

I'm not recieving any errors, its displaying the 'well done your entered into the database' type message from the success part of the IF statement, but not actually doing it.

 

 

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.