Getting a "Couldn't execute insert query", needing help!

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
ttsprez
Forum Newbie
Posts: 1
Joined: Tue Jul 14, 2009 5:47 am

Getting a "Couldn't execute insert query", needing help!

Post by ttsprez »

:banghead:
I know this is bound to be something very simple, but I'm so new to Php and struggling a bit. I've been working with this particular piece of code for these pages for almost a week and a half now. I'm proud of myself, cause I feel like I've picked up this stuff quite well for not knowing anything about it two weeks ago.

This is my situation. I've taken some code for a login and registration php page and actually got it to submit info as well as request username and login to start a session just fine. I even went as far as separating the code into two separate .php pages with their corresponding _form.inc pages and they still work just fine.

Next, I took those pages and adjusted them to my needs so that customers would be able to submit information I needed instead of the information that was laid out originally. All fields seem to be excepting the inputed data just fine on both pages when the submit button is hit because I'm not getting back any invalid format or blank errors.

What I do get however is a simple message that states "Couldn't execute insert query"; that's it. No line notice or anything else. I've check the code against the pages that are functioning and still can't seem to figure what I'm missing.

The directory of docs is located at:
http://www.thetitlestor.com/login

The files located in the /booklogin/ folder are the files that function just fine. They are the originals that I started with and separated. Input info and test them if you'd like


The files in the /ttscustvisit/ folder are the files that I've tweaked to have the customer provide the info in the format I need. When pulled up in the browser they come up just fine. They allow for inputing just fine as well,

The following is the customer registration page:
http://www.thetitlestor.com/login/ttscu ... egonly.php

This is the php code I'm currently using on this page:

<?php
/* Program: ttscustregonly.php
* Desc: Registration program for
* thethestor.com. It provides one options:
* (1) registration of contact info for site visitation.
*
*/
session_start();
include("ttscustvisitdogs.php");
switch (@$_POST['do'])
{
case "new":
/* Check for blanks */
foreach($_POST as $field => $value)
{
if ($field != "fax")
{
if ($value == "")
{
$blanks[] = $field;
}
}
}
if(isset($blanks))
{
$message_new = "The following fields are blank.
Please enter the required information: ";
foreach($blanks as $value)
{
$message_new .= "$value, ";
}
extract($_POST);
include("ttscustregonly_form.inc");
exit();
}

/* Validate data */
foreach($_POST as $field => $value)
{
if(!empty($value))
{


if(eregi("name",$field) and
!eregi("login",$field))
{
if (!ereg("^[A-Za-z' -]{1,50}$",$value))
{
$errors[]="$value is not a valid name.";
}
}


if(eregi("state",$field))
{
if(!ereg("[A-Za-z]{2}",$value))
{
$errors[]="$value is not a valid state.";
}
}


if(eregi("phone",$field))
{
if(!ereg("^[0-9)(xX -]{7,20}$",$value))
{
$errors[] = "$value is not a valid
phone number. ";
}
}


if(eregi("mail",$field))
{
if(!ereg("^.+@.+\\..+$",$value))
{
$errors[] = "$value is not a valid
email address.";
}
}

} // end if empty

} // end foreach

if(@is_array($errors))
{
$message_new = "";
foreach($errors as $value)
{
$message_new .= $value." Please try
again<br />";
}
extract($_POST);
include("ttscustregonly_form.inc");
exit();
}

/* clean data */
$cxn = mysqli_connect($host,$user,$passwd,$dbname);

foreach($_POST as $field => $value)
{
if($field != "Button" and $field != "do")
{
if($field == "password")
{
$password = strip_tags(trim($value));
}
else
{
$fields[]=$field;
$value = strip_tags(trim($value));
$values[] =
mysqli_real_escape_string($cxn,$value);
$$field = $value;
}
}
}

/* check whether user name already exists */
$sql = "SELECT loginName FROM Member
WHERE loginName = '$loginName'";
$result = mysqli_query($cxn,$sql)
or die("Couldn't execute select query.");
$num = mysqli_num_rows($result);
if ($num > 0)
{
$message_new = "$loginName already used.
Select another User Name.";
include("ttscustregonly_form.inc");
exit();
}
/* Add new member to database */
else
{
$today = date("Y-m-d");
$fields_str = implode(",",$fields);
$values_str = implode('","',$values);
$fields_str .=",createDate";
$values_str .='"'.",".'"'.$today;
$fields_str .=",password";
$values_str .= '"'.","."md5"."('".$password."')";
$sql = "INSERT INTO Member ";
$sql .= "(".$fields_str.")";
$sql .= " VALUES ";
$sql .= "(".'"'.$values_str.")";
$result = mysqli_query($cxn,$sql)
or die("Couldn't execute insert query.");
$_SESSION['auth']="yes";
$_SESSION['logname'] = $loginName;

/* send email to new member */
$emess = "A new Customer Access Account has been setup. ";
$emess.= "Your new Username and Password are: ";
$emess.= "\n\n\t$loginName\n\t$password\n\n";
$emess.="We appreciate your interest in ";
$emess.= " THETITLESTOR.COM. \n\n";
$emess.= "If you have any questions or problems,";
$emess.= " email us at questionsandconcerns@thetitlestor.com";
$ehead="From: ttsprez@thetitlestor.com\r\n";
$subj = "Your new Customer Access Account informatin from THETITLESTOR.COM";
$mailsnd=mail("$email","$subj","$emess","$ehead");
header("Location: loginonly.php");
}
break;
default:
include("ttscustregonly_form.inc");
}
?>

if you'd like to see the source code for the ttscustregonly_form.inc, simply right click and hit view source on the ttscustregonly.php page, it will display it.

The other page is located in the same directory under ttscustvehinfo.php and it's code is a follows:

<?php
/* Program: ttscustvehinfo.php
* Desc: Registration program for
* thethestor.com. It provides one options:
* (1) registration of contact info for site visitation.
*
*/
session_start();
include("ttscustvisitdogs.php");
switch (@$_POST['do'])
{
case "new":


/* Validate data */
foreach($_POST as $field => $value) {
if(!empty($value))
{


if(eregi("name",$field) and
!eregi("login",$field))
{
if (!ereg("^[A-Za-z' -]{1,50}$",$value))
{
$errors[]="$value is not a valid name.";
}
}


if(eregi("state",$field))
{
if(!ereg("[A-Za-z]{2}",$value))
{
$errors[]="$value is not a valid state.";
}
}


if(eregi("phone",$field))
{
if(!ereg("^[0-9)(xX -]{7,20}$",$value))
{
$errors[] = "$value is not a valid
phone number. ";
}
}


if(eregi("mail",$field))
{
if(!ereg("^.+@.+\\..+$",$value))
{
$errors[] = "$value is not a valid
email address.";
}
}

} // end if empty

} // end foreach

if(@is_array($errors))
{
$message_new = "";
foreach($errors as $value)
{
$message_new .= $value." Please try
again<br />";
}
extract($_POST);
include("ttscustvehinfo_form.inc");
exit();
}

/* clean data */
$cxn = mysqli_connect($host,$user,$passwd,$dbname);

foreach($_POST as $field => $value)
{
if($field != "Button" and $field != "do")
{
if($field == "password")
{
$password = strip_tags(trim($value));
}
else
{
$fields[]=$field;
$value = strip_tags(trim($value));
$values[] =
mysqli_real_escape_string($cxn,$value);
$$field = $value;
}
}
}

/* check whether user name already exists */
$sql = "SELECT loginName FROM Member
WHERE loginName = '$loginName'";
$result = mysqli_query($cxn,$sql)
or die("Couldn't execute select query.");
$num = mysqli_num_rows($result);
if ($num > 0)
{
$message_new = "$loginName already used.
Select another User Name.";
include("ttscustvehinfo_form.inc");
exit();
}
/* Add new member to database */
else
{
$today = date("Y-m-d");
$fields_str = implode(",",$fields);
$values_str = implode('","',$values);
$fields_str .=",createDate";
$values_str .='"'.",".'"'.$today;
$fields_str .=",password";
$values_str .= '"'.","."md5"."('".$password."')";
$sql = "INSERT INTO Member ";
$sql .= "(".$fields_str.")";
$sql .= " VALUES ";
$sql .= "(".'"'.$values_str.")";
$result = mysqli_query($cxn,$sql)
or die("Couldn't execute insert query.");
$_SESSION['auth']="yes";
$_SESSION['logname'] = $loginName;

/* send email to new member */
$emess = "A new Customer Access Account has been setup. ";
$emess.= "Your new Username and Password are: ";
$emess.= "\n\n\t$loginName\n\t$password\n\n";
$emess.="We appreciate your interest in ";
$emess.= " THETITLESTOR.COM. \n\n";
$emess.= "If you have any questions or problems,";
$emess.= " email us at questionsandconcerns@thetitlestor.com";
$ehead="From: ttsprez@thetitlestor.com\r\n";
$subj = "Your new Customer Access Account informatin from THETITLESTOR.COM";
$mailsnd=mail("$email","$subj","$emess","$ehead");
header("Location: loginonly.php");
}
break;
default:
include("ttscustvehinfo_form.inc");
}
?>

if you'd like to see the source code for the ttscustvehinfo_form.inc, simply right click and hit view source on the ttscustregonly.php page, it will display it.

The connection info is located in the ttscustvisitdogs.inc file in this same folder and contains the following code connecting info:

<?php
$host="localhost";
$user="thetitl1_ttsprez";
$passwd = "test";
$dbname = "thetitl1_ttscustvisit";
?>

I really hope someone can take a look a this and tell me where I'm going wrong here. I really would appreciate the time and consideration. And if along the way it makes things easier for other newbies like myself, all the better. If it would be easier to go over this by phone with me I can provide my phone number upon request.

Again, let me say thanks in advance to the person or persons that will help me out tremendously.......
_________________ :? :?
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Getting a "Couldn't execute insert query", needing help!

Post by Mark Baker »

Post your code within code tags in future, it makes it a lot easier to read.

And add some code logic to display the query that you're executing and the actual error message you're getting - it helps a lot when trying to diagnose problems
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Getting a "Couldn't execute insert query", needing help!

Post by Eric! »

Since you didn't say exactly when or where in your code you get the generic insert error, I suggest you look at both segments.

There is probably a field not correct in the following

Code: Select all

$sql = "INSERT INTO Member ";
$sql .= "(".$fields_str.")";
$sql .= " VALUES ";
$sql .= "(".'"'.$values_str.")";
$result = mysqli_query($cxn,$sql)
or die("Couldn't execute insert query.");
echo out $sql and check it.

Same for this

Code: Select all

$sql = "INSERT INTO Member ";
$sql .= "(".$fields_str.")";
$sql .= " VALUES ";
$sql .= "(".'"'.$values_str.")";
$result = mysqli_query($cxn,$sql)
or die("Couldn't execute insert query.");
Check the formatting, correct field names and values. If you still can't get it, post the echoed strings back here for us to see.
Post Reply