Jump to content

Help with php code


jsucupira

Recommended Posts

This is my first time trying to use PHP so I am not sure what I am doing wrong in here

 

----------------------------------------------------------------------------------------------------------

<?php

extract ($_post);

$iserror = false;

 

$genderlist = array("m" => "Male", "f" => "Female");

 

$inputlist = array("fname" => "First Name","lname" => "Last Name", "email" => "Email", "login" => "Login Name", "password" => "Password","street" => "Street",

"city" => "City", "state" => "State", "zip" => "Zip Code", "phone" => "Phone");

if (isset ($submit)){

if ($fname ==""){

$formerros["fnameerror"] = true;

$iserror = true;

}

 

if ($lname ==""){

$formerros["lnameerror"] = true;

$iserror = true;

}

if ($email ==""){

$formerros["emailerror"] = true;

$iserror = true;

}

if (!ereg("^\([0-9]{3}\)[0-9]{3}-{0-9}{4}$", $phone)){

$formerrors["phoneerror"] = true;

$iserror = true;

}

if (!$iserror){

 

//Insert Query

$query = "insert into users" .

"(first_name, last_name, email, login, password, street, city, state, zip, gender, dob, phone)" .

"values ('$lname', '$fname', '$email','$login', '$password','$street', '$city', '$state', '$zip','$gender', '$dob', '$phone', " .

"'" . quotemeta($phone). "');

//connect to mysql

if (!($database = mysql_connect("localhost","root","ace","")))

die("Could not connect");

 

//open database

if (!mysql_select_db("tallynightlife", $database))

die("Could not connect to tally");

 

//execute query

if (!($result = mysql_query( $query, $database))){

print("could not execute");

die(mysql_error());

}

 

print( "<table>

<tr>

<td>Name</td>

<td>email</td>

<td>login</td>

<td>street</td>

<td>city</td>

<td>state</td>

<td>zip</td>

<td>gender</td>

<td>dob</td>

<td>password</td>

<td>Phone</td></tr>

<tr>

<td>$fname  $lname</td>

<td>$email</td>

<td>$login</td>

<td>$street</td>

<td>$city</td>

<td>$state</td>

<td>$zip</td>

<td>$gender</td>

<td>$dob</td>

<td>$password</td>

<td>$Phone</td></tr></table>");

die();

}

}

print("<h1>This is the sample</h1>");

 

if (&iserror) {

print("<span>Fields with * need to be filled.</span>" );

}

 

print("<!-- post form data to form.php -->

<form method ='post' action = 'library/form.php'><br />);

 

foreach($inputlist as $inputname => $inputalt ) {

$inputtext = $inputvalues [$inputname ];

 

print("<input type ='text' name ='$inputname' value = '" . $$inputname. "' />);

if ( $formerrors[ ($inputname)."error" ] ==true );

print("<br />);

}

 

if ( $formerrors["phoneerror"]);

print("'>Must be in the form (555)555-5555

<br />

 

<span style ='color:blue'>Gender</span><br />

 

<select name='gender'>");

 

foreach( $genderlist as $currgender){

print("<option");

if (( $currgender == $book))

print( "selected = 'true'");

print(">$currgender</option>");

}

 

print("<input type = 'Submit' name = 'submit' value='register' />

</form></body></html>);

?>

 

 

also with this one

 

---------------------------------------------------------------------------------------

 

<?php

if(isset($_POST['add']))

{

include 'library/config.php';

include 'library/opendb.php';

 

$username = $_POST['login'];

$password = $_POST['password'];

 

$query = "INSERT INTO login (login, password) VALUES ('$username','$password')";

mysql_query($query) or die('Error, insert query failed');

 

$query = "FLUSH PRIVILEGES";

mysql_query($query) or die('Error, insert query failed');

 

include 'library/closedb.php';

echo "New MySQL user added";

}

else

{

 

<form method="post">

<table width="400" border="0" cellspacing="1" cellpadding="2">

<tr>

<td width="100">Username</td>

<td><input name="username" type="text" id="login"></td>

</tr>

<tr>

<td width="100">Password</td>

<td><input name="password" type="text" id="password"></td>

</tr>

<tr>

<td width="100"> </td>

<td> </td>

</tr>

<tr>

<td width="100"> </td>

<td><input name="add" type="submit" id="add" value="Add New User"></td>

</tr>

</table>

</form>

?>

 

 

----------------------------------------------------------------------

 

I appreciate the help.. thnx

 

Jonathas

Link to comment
https://forums.phpfreaks.com/topic/67091-help-with-php-code/
Share on other sites

The first block of code you posted has a never ending amount of errors...but your second block of code didn't have much.

 

Here is your second block of code with no syntax errors:

<?php
if (isset($_POST['add'])) {
    include 'library/config.php';
    include 'library/opendb.php';
    
    $username = $_POST['login'];
    $password = $_POST['password'];
    
    $query = "INSERT INTO login (login, password) VALUES ('$username','$password')";
    mysql_query($query) or die('Error, insert query failed');
    
    $query = "FLUSH PRIVILEGES";
    mysql_query($query) or die('Error, insert query failed');
    
    include 'library/closedb.php';
    echo "New MySQL user added";
} else {
    ?>
    <form method="post">
    <table width="400" border="0" cellspacing="1" cellpadding="2">
    <tr>
    <td width="100">Username</td>
    <td><input name="username" type="text" id="login"></td>
    </tr>
    <tr>
    <td width="100">Password</td>
    <td><input name="password" type="text" id="password"></td>
    </tr>
    <tr>
    <td width="100"> 
    </td>
    <td> 
    </td>
    </tr>
    <tr>
    <td width="100"> 
    </td>
    <td><input name="add" type="submit" id="add" value="Add New User"></td>
    </tr>
    </table>
    </form>
<? }
?>


Link to comment
https://forums.phpfreaks.com/topic/67091-help-with-php-code/#findComment-336496
Share on other sites

With the new code I still the error "PHP Parse error: syntax error, unexpected $end in C:\Inetpub\wwwroot\adduser.php on line 56"

 

can anyone help.

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

 

<title>New user</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>

 

<body>

 

<?php

if (isset($_POST['add'])) {

    include 'library/config.php';

    include 'library/opendb.php';

   

    $username = $_POST['login'];

    $password = $_POST['password'];

   

    $query = "INSERT INTO login (login, password) VALUES ('$username','$password')";

    mysql_query($query) or die('Error, insert query failed');

   

    $query = "FLUSH PRIVILEGES";

    mysql_query($query) or die('Error, insert query failed');

   

    include 'library/closedb.php';

    echo "New MySQL user added";

} else {

    ?>

    <form method="post">

    <table width="400" border="0" cellspacing="1" cellpadding="2">

    <tr>

    <td width="100">Username</td>

    <td><input name="username" type="text" id="login"></td>

    </tr>

    <tr>

    <td width="100">Password</td>

    <td><input name="password" type="text" id="password"></td>

    </tr>

    <tr>

    <td width="100"> 

    </td>

    <td> 

    </td>

    </tr>

    <tr>

    <td width="100"> 

    </td>

    <td><input name="add" type="submit" id="add" value="Add New User"></td>

    </tr>

    </table>

    </form>

<? }

?>

</body>

</html>

Link to comment
https://forums.phpfreaks.com/topic/67091-help-with-php-code/#findComment-336933
Share on other sites

Strange, when I upload it to my server I get no errors. Make sure you have a text editor that isn't going to count white spaces as code, that could mess the script up.

 

Try this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<title>New user</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

<?php
if (isset($_POST['add'])) {
    include 'library/config.php';
    include 'library/opendb.php';
   
    $username = $_POST['login'];
    $password = $_POST['password'];
   
    $query = "INSERT INTO login (login, password) VALUES ('$username','$password')";
    mysql_query($query) or die('Error, insert query failed');
   
    $query = "FLUSH PRIVILEGES";
    mysql_query($query) or die('Error, insert query failed');
   
    include 'library/closedb.php';
    echo "New MySQL user added";
} else {
?>
    <form method="post">
    <table width="400" border="0" cellspacing="1" cellpadding="2">
    <tr>
    <td width="100">Username</td>
    <td><input name="username" type="text" id="login"></td>
    </tr>
    <tr>
    <td width="100">Password</td>
    <td><input name="password" type="text" id="password"></td>
    </tr>
    <tr>
    <td width="100"> 
    </td>
    <td> 
    </td>
    </tr>
    <tr>
    <td width="100"> 
    </td>
    <td><input name="add" type="submit" id="add" value="Add New User"></td>
    </tr>
    </table>
    </form>
<?php 
}
?>
</body>
</html>

 

I didn't change much, but that might make the difference. Also, make sure you use [code.][./code] next time (without the dots).

Link to comment
https://forums.phpfreaks.com/topic/67091-help-with-php-code/#findComment-336940
Share on other sites

Sry for been so newb, but could someone tell me what's wrong with these 3 codes. when I try the code that pocobueno1388 helped me with it said "PHP Fatal error: Call to undefined function mysql_connect() in C:\Inetpub\wwwroot\library\opendb.php on line 11"

 

 

<?php

// This is an example opendb.php

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die                      ('Error connecting to mysql');

mysql_select_db($dbname);

?>

 

--------------------------------------------------------

 

 

<?php

 

 

mysql_close($conn);

?>

 

-------------------------------

 

 

<?php

 

$dbhost = 'localhost';

$dbuser = 'root';

$dbpass = 'ace';

$dbname = 'tallynightlife';

?>

 

Link to comment
https://forums.phpfreaks.com/topic/67091-help-with-php-code/#findComment-336950
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.