PDA

View Full Version : Need some PHP/MySQL help


WildWayz
17-01-2001, 10:21
Hi ya

I have been writing some PHP stuff last night considering my friggin ISDN is out of action until BT fix the cabling.

So far, I have written a script to authorise users that are in a MySQL database - that works fine. The problem I have at the moment, is that I am making an add new users script that has checking to see if a username is already in the database, and if there is, it gets u to re-enter the info again.

Now, the problem is that it does this ok, but it isn't writing the data to the mysql database if the username is unique.
It worked before I done this checking tho.

The code is

<?
require("config.php");

if ((!$first_name) || (!$last_name) || (!$username) || (!$password)) {
header("Location: view_adduser.php");
exit;
}

$table_name = "users";

$connection = @mysql_connect("$servername", "$dbusername", "$dbpassword")
or die("Couldn't connect.");

$db = mysql_select_db($dbname, $connection)
or die("Couldn't select database.");


$chk_id = "SELECT username FROM $table_name WHERE username = \"$username\"";

$chk_id_res = @mysql_query($chk_id,$connection) or
die("Couldn't execute query.");

$chk_id_num = mysql_num_rows($chk_id_res);

if ($chk_id_num != "0") {
header("Location: view_adduser.php");
exit;
} else
{
$sql = "INSERT INTO $table_name (id, first_name, last_name, username, password, email_address, enroll_date, status) VALUES (\"\", \"$first_name\", \"$last_name\", \"$username\", password(\"$password\"), \"$email_address\", \"$enroll_date\", \"$status\")";

$result = mysql_query($sql,$connection)
or die("Couldn't execute query.");

}

?>

<HTML>
<HEAD>
<TITLE>Add a New User</TITLE>
</HEAD>
<BODY>

<H1>Added the user:</H1>

<P><STRONG>First Name:</STRONG><BR>
<? echo "$first_name"; ?></p>

<P><STRONG>Last Name:</STRONG><BR>
<? echo "$last_name"; ?></p>

<P><STRONG>Username:</STRONG><BR>
<? echo "$username"; ?></p>

<P><STRONG>Password:</STRONG><BR>
<? echo "$password"; ?></p>

<P><STRONG>Password:</STRONG><BR>
<? echo "$email_address"; ?></p>

<P><STRONG>Password:</STRONG><BR>
<? echo "$enroll_date"; ?></p>

<P><STRONG>Password:</STRONG><BR>
<? echo "$status"; ?></p>

<P><a href="view_adduser.php">Add Another</a></p>

</BODY>
</HTML>



Any help would be appreciated! :)

--WildWayz

Vampire
17-01-2001, 11:04
I think the problem is because you insert "" (=NULL) into the ID.
Is ID auto-increment and unique? if it is, just omit it.

I'm really not sure, but I do know that playing with "" (empty) values is dangerous. :)

WildWayz
17-01-2001, 11:37
ID is an auto incremented field.

So... what would u advise?

--WildWayz

Vampire
17-01-2001, 11:46
I'm not sure, but try this:

$sql = "INSERT INTO $table_name (first_name, last_name, username, password, email_address, enroll_date, status) VALUES (\"$first_name\", \"$last_name\", \"$username\", password(\"$password\"), \"$email_address\", \"$enroll_date\", \"$status\")";

so its the insert without the ID, because the ID is filled in by MySql...........:E

let me know if it worked. :E

:vampire:

WildWayz
17-01-2001, 11:54
Ta - will try it out at home tonight :)

--WildWayz

Baring in mind I have no ISDN/Phone at home until BT fix the underground cabling they sliced through.

Vampire
17-01-2001, 12:17
n/p.

But if you don't have internetconnection , how do you test then? Did you installed PHP local???????

Perhaps you know it, but http://www.zend.com is the most complete site about PHP/mysql. have a look there.......

[Edited by Vampire on 17-01-2001 at 11:18 AM]

WildWayz
17-01-2001, 12:22
yeah - I have PHP3, mysql and apache installed on my home win2k PC :)

I wish I could install PHP4.04 on my dedicated server, but they are running RedHat 5.2 and PHP requires all kinda kernal updates etc :/

--WildWayz

Skunk
17-01-2001, 14:37
Try using the alternative form of update - also putting stuff in single rather than double quotes in the SQL saves you having to use ugly \" all the time:
$sql = "INSERT INTO $table_name SET
first_name = '$first_name',
last_name = '$last_name',
username = '$username',
password = password('$password'),
email_address = '$email_address',
enrol_date = '$enrol_date',
status = '$status'";

Should work fine.

Vampire
17-01-2001, 14:49
thats indeed an alternative way, but i think that omiting the decaration of the fields that need to be inserted/updated, will only work if you insert into all the fields.

So I think this is the best syntax:
$sql = "INSERT INTO $table_name (first_name, last_name, username, password, email_address, enroll_date, status) VALUES ('$first_name', '$last_name', '$username', password('$password'), '$email_address', '$enroll_date', '$status')";

WildWayz
17-01-2001, 18:56
Damn - still didn't add it to the MySQL database :/

It loops around ok, just doesn't write it to the database :/

--WildWayz

WildWayz
17-01-2001, 19:29
Skunk - your code snippet gets a "could not exicute query" error :)

Turns out my code was fine - it was admin error ie I didn't know that phpMyAdmin ONLY shows 30 records at the time - no wonder I couldn't see it adding up :)

--WildWayz

Brocken
17-01-2001, 20:02
WW j00 :monkee: :P

WildWayz
06-02-2001, 10:40
For Ostrich

Compare it u monkee

--WildWayz

[Ostrich]
06-02-2001, 13:34
Sir!

:)