Jump to content

Add User script "Could not execute query"


okrobie

Recommended Posts

This should be an easy script but I can't get it to run. Can someone please help me?

 

<html><head><title>Adding a User</title></head>
<body>
<?php

$self = $_SERVER['PHP_SELF'];
$first_name = $_POST['firstname'];
$last_name = $_POST['lastname'];
$user_email = $_POST['useremail'];

if( (!$first_name) or (!$last_name) or (!$user_email)  )
{
  $form ="Please enter all new user details...";
  $form.="<form action=\"$self\"";
  $form.=" method=\"post\">First Name: ";
  $form.="<input type=\"text\" name=\"firstname\"";
  $form.=" value=\"$first_name\"><br>Last Name: ";
  $form.="<input type=\"text\" name=\"lastname\"";
  $form.=" value=\"$last_name\"><br>Email: ";
  $form.="<input type=\"text\" name=\"useremail\"";
  $form.=" value=\"$user_email\"><br>";
  $form.="<input type=\"submit\" value=\"Submit\">";
  $form.="</form>";
  echo($form);
}
else
{
  #connect to MySQL
  $conn = @mysql_connect("localhost","xxxx","xxxx")
	        or die("Could not connect to MySQL");
  #select a database
  $db = @mysql_select_db("xxxx",$conn)
	        or die("Could not select database");
  #create the SQL query
  $sql = "insert into tablename (firstname, lastname, useremail)
  values (\"$first_name\",'\"$last_name\",\"$user_email\" )";  
  #execute the query
  $result = @mysql_query($sql,$conn)
	        or die("Could not execute query");
  if($result)
  { echo("New user $user_email added"); }
}
?>
</body></html>

Link to comment
Share on other sites

Thanks for your help.

 

I tried following the example given but I keep getting unexpected T_ECHO

 

I don't know the proper syntax

 

Why not read the documentation provided? Can't be that hard.

  $result = mysql_query($sql,$conn) or die("Could not execute query: ". mysql_error());

 

 

Link to comment
Share on other sites

I have installed the mysql_error() code, but I'm not getting any error messages from it.

Still getting "Could not execute query"  I also re-built the table.

 

<html><head><title>Adding a User</title></head>
<body>
<?php

$self = $_SERVER['PHP_SELF'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$user_email = $_POST['user_email'];

if( (!$first_name) or (!$last_name) or (!$user_email)  )
{
  $form ="Please enter all new user details...";
  $form.="<form action=\"$self\"";
  $form.=" method=\"post\">First Name: ";
  $form.="<input type=\"text\" name=\"first_name\"";
  $form.=" value=\"$first_name\"><br>Last Name: ";
  $form.="<input type=\"text\" name=\"last_name\"";
  $form.=" value=\"$last_name\"><br>Email: ";
  $form.="<input type=\"text\" name=\"user_email\"";
  $form.=" value=\"$user_email\"><br>";
  $form.="<input type=\"submit\" value=\"Submit\">";
  $form.="</form>";
  echo($form);
}
else
{
  #connect to MySQL
  $conn = @mysql_connect("localhost","username","pwd")
	        or die("Could not connect to MySQL");
  #select a database
  $db = @mysql_select_db("dbname",$conn)
	        or die("Could not select database");
  #create the SQL query
  $sql = "insert into tablename (first_name, last_name, user_email)
  values (\"$first_name\",'\"$last_name\",\"$user_email\" )";  
  echo mysql_errno($conn) . ": " . mysql_error($conn) . "\n";
  #execute the query
  $result = @mysql_query($sql,$conn)
   		or die("Could not execute query");
	        if($result)
  { echo("New user $user_email added"); }
  echo mysql_errno($conn) . ": " . mysql_error($conn) . "\n";

}
?>
</body></html>

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.