Jump to content

Recommended Posts

hi im trying to run this basic code to add users to a table.

 

It keeps returning the result "could not exectute query". My guess is the "insert into users" query has some type of error.

 

I am using a database called moksha, which i have previously created. And i am also using the basic code underneath this code to create the table. Both the database and table have been successfully created.

 

feel free to create the database and test the code yourself.

 

<!-- example for PHP 5.0.0 final release -->

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

$self = $_SERVER['PHP_SELF'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$username = $_POST['username'];
$password = $_POST['password'];

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

 

Here is the code i used which allows me to create the user table.

 


<!-- example for PHP 5.0.0 final release -->

<html><head><title>Creating a table</title></head>
<body>
<?php

$self = $_SERVER['PHP_SELF'];
$fields = $_POST['fields'];
$db =     $_POST['db'];
$name =   $_POST['name'];
$table =  $_POST['table'];
$type =   $_POST['type'];
$size =   $_POST['size'];


if( !$fields and !$db )
{
  $form ="<form action=\"$self\" method=\"post\">";
  $form.="How many fields are needed in the new table?<br>";
  $form.="<input type=\"text\" name=\"fields\" size=\"5\">";
  $form.="<input type=\"submit\" value=\"Submit\">";
  echo($form);
}
else if( !$db )
{ 
  $form ="<form action=\"$self\" method=\"post\">";
  $form.="Database:     <input type=\"text\" name=\"db\"><br>";
  $form.="Table Name:  <input type=\"text\" name=\"table\" size=\"10\"><br> ";
  for ($i = 0 ; $i <$fields; $i++) 
  {
    $form.="Column Name:<input type=\"text\" name=\"name[$i]\" size=\"10\"> ";
    $form.="Type: <select name=\"type[$i]\">";
    $form.="<option value=\"char\">char</option>";	
    $form.="<option value=\"varchar\">varchar</option>";
    $form.="<option value=\"int\">int</option>";
    $form.="<option value=\"float\">float</option>";
    $form.="<option value=\"timestamp\">timestamp</option>";
    $form.="</select> ";
    $form.="Size:<input type=\"text\" name=\"size[$i]\" size=\"5\"><br>";
  }
  $form.=" <input type=\"submit\" value=\"Submit\"></form>";
  echo($form);
}
else
{
  $conn = @mysql_connect("localhost", "root", "alpha")
or die("Could not connect.");

  $rs = @mysql_select_db($db, $conn)
or die("Could not select database.");

  $num_columns = count($name);

  $sql = "create table $table (";
  for ($i = 0; $i < $num_columns; $i++) 
  {
    $sql .= "$name[$i] $type[$i]";
    if(($type[$i] =="char") or ($type[$i] =="varchar"))
    {
      if($size[$i] !="" ){ $sql.= "($size[$i])"; }
    }
    if(($i+1) != $num_columns){ $sql.=","; }
  }
  $sql .= ")";

  echo("SQL COMMAND: $sql <hr>");

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

  if ($result) {     
echo("RESULT: table \"$table\" has been created");
  }
}

?>
</body></html>

 

Link to comment
https://forums.phpfreaks.com/topic/62832-add-user-script/
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.