Jump to content

Annoying syntax, Can anyone help fix it?


devang23

Recommended Posts

this is the code, and i keep getting a t string syntax on line 24

 

<html><head><title>Adding a User </title></head>
<body>
<?php
if( (!$firstname) or (!$lastname) or (!$username) or (!$password) )
{
$form ="Please enter all new user details";
$form.="<form action=\"$PHP_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>UserName: ";
$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
{ $conn = mysql_connect("mysql4.000webhost.com")or die("Try Again Punk");
  $db = mysql_select_db("a3213677_snorre",$conn)or die("NOOPE");
  $sql = insert into User Table(first_name,last_name,user_name,password) values(\"$firstname\",\"$lastname\",\"$username\",password(\$password\") )";
  $result = mysql_query($sql,$conn)or die ("NOT EVEN");
  if($result) { echo("New User $username ADDED DUH!"); }
}
?> </body></html>

Link to comment
Share on other sites

You had some mistakes with your quotes when defining your $sql variable. Try:

 

$sql = "insert into User Table(first_name,last_name,user_name,password) values('$firstname','$lastname','$username',password('$password'))";

 

I'd also suggest you don't use the MySQL PASSWORD() function to store your passwords. It has changed in the past and it's a possibility that it will change again, and if it does it would break your application.

Link to comment
Share on other sites

You had some mistakes with your quotes when defining your $sql variable. Try:

 

$sql = "insert into User Table(first_name,last_name,user_name,password) values('$firstname','$lastname','$username',password('$password'))";

 

I'd also suggest you don't use the MySQL PASSWORD() function to store your passwords. It has changed in the past and it's a possibility that it will change again, and if it does it would break your application.

 

WOW! your the first person that got it to show up without a syntax....but now, when i put the information in, it doesnt respond with something that says the data was created....how would this info pop up into mysql?

Link to comment
Share on other sites

Are you getting your success message? If it's not updating the database try this:

 

$result = mysql_query($sql,$conn) or trigger_error(mysql_error());

 

Is your table named 'User Table' (It is case-sensitive)?

Link to comment
Share on other sites

Are you getting your success message? If it's not updating the database try this:

 

$result = mysql_query($sql,$conn) or trigger_error(mysql_error());

 

Is your table named 'User Table' (It is case-sensitive)?

 

Yes, it is called "User Table" just like that, the capital U and capital T...im sure im a couple of steps away from making this work.

Link to comment
Share on other sites

Are you getting your success message? If it's not updating the database try this:

 

$result = mysql_query($sql,$conn) or trigger_error(mysql_error());

 

Is your table named 'User Table' (It is case-sensitive)?

 

Yes, it is called "User Table" just like that, the capital U and capital T...im sure im a couple of steps away from making this work.

 

 

and yes i didnt try the new code you suggested, but same thing, it just reloads the page

Link to comment
Share on other sites

To show all php errors, add the following two lines of code immediately after your first opening <?php tag -

ini_set("display_errors", "1");
error_reporting(E_ALL);

 

The normal action of trigger_error() won't show anything on a majority of the php systems in use.

Link to comment
Share on other sites

By just reloads the page do you mean that it's still displaying the form? That would mean that it's not even getting to the mysql insert, which could be why it's not working. $username, $password, won't even be set unless register_globals is enabled on your server (which is a bad idea..). Try this:

 

<html><head><title>Adding a User </title></head>
<body>
<?php
if( (!isset($_POST['username'], $_POST['password'], $_POST['lastname'], $_POST['firstname']) )
{
$form ="Please enter all new user details";
$form.="<form action=\"$PHP_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>UserName: ";
$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
{ $conn = mysql_connect("mysql4.000webhost.com")or die("Try Again Punk");
  $db = mysql_select_db("a3213677_snorre",$conn)or die("NOOPE");
$sql = "insert into `User Table` (first_name,last_name,user_name,password) values('{$_POST['firstname']}','{$_POST['lastname']}','{$_POST['username']}',password('{$_POST['password']}'))";
  $result = mysql_query($sql,$conn)or die ("NOT EVEN");
  if($result) { echo("New User $username ADDED DUH!"); }
}
?> </body></html>

Link to comment
Share on other sites

Correct, it just keeps refreshing the form....and when i put the code you just sent there was a syntax error on line 5, and i still didnt catch it.  could it be because it cant find the mysql server?  if so i dont understand why it wont, all the info i have is right

Link to comment
Share on other sites

Sorry, I left an extra ( when I edited your code.

 

<html><head><title>Adding a User </title></head>
<body>
<?php
if( !isset($_POST['username'], $_POST['password'], $_POST['lastname'], $_POST['firstname']) )
{
$form ="Please enter all new user details";
$form.="<form action=\"$PHP_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>UserName: ";
$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
{ $conn = mysql_connect("mysql4.000webhost.com")or die("Try Again Punk");
  $db = mysql_select_db("a3213677_snorre",$conn)or die("NOOPE");
$sql = "insert into `User Table` (first_name,last_name,user_name,password) values('{$_POST['firstname']}','{$_POST['lastname']}','{$_POST['username']}',password('{$_POST['password']}'))";
  $result = mysql_query($sql,$conn)or die ("NOT EVEN");
  if($result) { echo("New User $username ADDED DUH!"); }
}
?> </body></html>

Link to comment
Share on other sites

Taking another look over your code, I noticed some other errors. So I decided to rewrite it (somewhat) for you and explain it a little better. Hopefully this will teach you something for future use.

 

<html><head><title>Adding a User </title></head>
<body>
<?php
if(!isset($_POST['submit']))
{
echo <<<FORM
Please enter all new user details
<form action="" method="post">
First Name: <input type="text" name="firstname"><br>
Last Name: <input type="text" name="lastname"><br>
UserName: <input type="text" name="username"><br>
PassWord: <input type="text" name="password"><br>
<input type="submit" name="submit" value="Submit">
</form>
FORM;
}
else
{ 
$conn = mysql_connect("mysql4.000webhost.com")or die("Try Again Punk");
$db = mysql_select_db("a3213677_snorre",$conn)or die("NOOPE");
$sql = "insert into `User Table` (first_name,last_name,user_name,password) values('{$_POST['firstname']}','{$_POST['lastname']}','{$_POST['username']}',password('{$_POST['password']}'))";
$result = mysql_query($sql,$conn) or trigger_error(mysql_error());
if($result) { echo("New User $username ADDED DUH!"); }
}
?> </body></html>

 

It's much easier to write a block of html using the heredoc syntax. You had a problem with a missing " which is why it wasn't working before. You most likely missed this because it's hard to see when you're escaping tons of quotes. This way you can write the form like you normally would in any html document.

 

I also removed all the value="..."s because assuming this works you'll never be submitting data in a way that it'll be redisplayed on the form (Unless you add validation, which you don't currently have, but I'd suggest). I also removed the $PHP_SELF, because using this leaves you open to XSS attacks. It's a better idea to just leave the action attribute empty, or just type in the name of the file (if you want it to validate) if you want to submit a form back to the same page.

 

Finally I gave the submit button a name of 'submit', this way you can just use if(!isset($_POST['submit'])) { ... } to see if the form was submitted. ( isset ).

 

Hopefully this helps. ( And for future reference it's a better idea to keep this inside of the help topic, and not over pms ).

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.