Can't insert first row using PHP script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shb
Forum Newbie
Posts: 3
Joined: Fri Dec 10, 2010 1:39 am

Can't insert first row using PHP script

Post by shb »

I'm new to PHP/MySQL and have been searching high and low for a solution to this...

When using my PHP script to insert a row into the database it errors out but does not give me a specific error. When I use the MySQL command line client I can insert the first, second, third, or any number of rows without a problem.

What I don't understand is, once I have inserted a row using the command line client, my PHP script will insert rows without issue.

So my question is, what is it about the first row being inserted that makes it so special? One of the columns in the table is an AUTO_INCREMENT field, so i'm guessing that has something to do with it?
mellowman
Forum Commoner
Posts: 62
Joined: Sat Nov 22, 2008 5:37 pm

Re: Can't insert first row using PHP script

Post by mellowman »

the auto increment has no issues to it...i use them all the time :D

im going to go out on a llimb here and say that your basically asking how to insert into a row...plain and simple.

Here is how i would do it..if you get an error with this...then your problem is else where :banghead:

Code: Select all

$con = mysql_connect("HOSTCONNECTION","DATABASE","PASSWORD");
if (!$con){ die('Could not connect: ' . mysql_error());}
mysql_select_db("DATABASE", $con);

$sql = "INSERT INTO TABLENAME(Row 1,Row2,Row3)
VALUES ( 'value_row1','value_row2',value_row3')";

!mysql_query($sql,$con)){die('Error: ' . mysql_error());

mysql_close($con);
shb
Forum Newbie
Posts: 3
Joined: Fri Dec 10, 2010 1:39 am

Re: Can't insert first row using PHP script

Post by shb »

I have inserted rows using the MySQL command line client without issue. Here is my PHP code:

Code: Select all

<?php
include("config.php");
include("functions.php");

$usrname=mysql_real_escape_string($_POST['user']);
$psswd=md5(mysql_real_escape_string($_POST['pass']));
$acctype=$_POST['account'];
$first=mysql_real_escape_string($_POST['first']);
$last=mysql_real_escape_string($_POST['last']);
$phone=$_POST['phone'];
$email=mysql_real_escape_string($_POST['email']);

if (is_null($acctype))
{
	$acctype = 0;
}

$con = mysql_connect($dbhost,$dbuser,$dbpass);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db($db,$con) or die("Unable to select database");

if(($usrname == '')||(is_null($usrname)))
{
	echo "Your user name can not be blank or is already in use, please try again.";
}
else if(strlen($psswd) > 4)
{
	$query = "INSERT INTO User (Username, Password, Type) VALUES ('$usrname','$psswd','$acctype')";
	$result = mysql_query($query,$con);// or die('$result'.mysql_error());
	if(!$result) 
	{
		echo "Error in query: " .$query;
	}
	$uid = mysql_insert_id();

	if($acctype != 1)
	{	
		$query = "INSERT INTO Student (SID, Firstname, Lastname, Phone, Email) VALUES ('$uid','$first','$last','$phone','$email')";
		mysql_query($query,$con);
	}

	else
	{
		echo "Your account is now active";
	}
}
else
{
	echo "Your password does not meet the minimum length requirement, please try again.";
}

mysql_close($con);
?> 

However, the echo "Error in query: " .$query; line doesn't produce an error, it just shows the value of the query. I tried using mysql_error() and that also doesn't provide an error. If I enter a value into the table using MySQL command line, then the script works no problem.
shb
Forum Newbie
Posts: 3
Joined: Fri Dec 10, 2010 1:39 am

Re: Can't insert first row using PHP script

Post by shb »

I figured out the problem. I was calling a function which wasn't working properly.

Consider this solved.
Post Reply