Jump to content

Checking and Updating DB


jimmyp3016

Recommended Posts

Hey Guys,

Im having a problem with one of my scripts. Whenever I submit to this form it always errors out at error2.html even though the username is not in the database. What could be wrong?

[code]
<?php
include "../config-site.php";

// Open database...
$db = mysql_connect("$localhost", "$databaseuser", "$databasepasswd");
mysql_select_db("$databasename",$db);


// Check and Update affiliate data...
$sql="SELECT userName FROM user WHERE userName='$user'";
$result = mysql_query("$sql",$db);
if (mysql_num_rows($result) != 0) {
  header("Location:error2.html");
  exit;
}

$sql = "SELECT * FROM user WHERE id='$userID'";
$sql = "UPDATE user SET firstName = '$firstname', lastName = '$lastname', phone = '$phone', company = '$company', website = '$website', aboutme = '$aboutme' WHERE id='$userID'";
$result = mysql_query($sql,$db) || die("Can't query DB: ". mysql_error());


// Close database...

mysql_close($db);
?>[/code]

Any help would be great thanks!
Link to comment
Share on other sites

Hey Thorpe,

Thanks for responding so fast.

I define user up at the top. (sorry i didnt put it in)  $user =  $_GET['userName'];

I verified that it is actually grabbing it to. Do you know what could be the problem?

It gets hung up on the check and errors out to error2.html everytime.

Ive been troubleshooting for about an hour.

Any help would be great thanks.
Link to comment
Share on other sites

Here is my whole script so we can get to the bottom of this.

[code]<?php

$userID = $_GET['id'];
$user = $_GET['userName'];
$firstname = $_GET['firstName'];
$lastname = $_GET['lastName'];
$address             = $_GET['address'];
$city = $_GET['city'];
$state = $_GET['state'];
$zip = $_GET['zip'];
$phone = $_GET['phone'];
$company = $_GET['company'];
$website             = $_GET['website'];
$aboutme = $_GET['aboutme'];


if ( ($user=="") || ($firstname=="") || ($lastname=="") || ($phone=="") || ($company=="") || ($website=="") )
{
    header("Location:error1.html");
    exit;
}


include "../config-site.php";
include 'corpmessage.php';

// Open database...
$db = mysql_connect("$localhost", "$databaseuser", "$databasepasswd");
mysql_select_db("$databasename",$db);


// Check and store affiliate data...
$sql="SELECT userName FROM user WHERE userName='$user'";
$result = mysql_query("$sql",$db);
if (mysql_num_rows($result) != 0) {
    header("Location:error2.html");
  exit;
}

$sql = "SELECT * FROM user WHERE id='$userID'";
$sql = "UPDATE user SET firstName = '$firstname', lastName = '$lastname', phone = '$phone', company = '$company', website = '$website', aboutme = '$aboutme' WHERE id='$userID'";
$result = mysql_query($sql,$db) || die("Can't query DB: ". mysql_error());


// Close database...

mysql_close($db);


// Send message to inform webmaster about the new affiliate...
$subject="$firstname $lastname has signed up with $sitename";
mail("$affiliaterecipient","$subject","$mailbody","From: $siteemail \nContent-Type: text/plain; charset=iso-8859-1\nContent-Transfer-Encoding: 8bit");


// Show thank you message...

header("Location:invite.php?id=$userID");
?>[/code]

Link to comment
Share on other sites

Thorpe, surely you need to define $result to use it in an if statement?

Jimmy, what exactly did you do to get this:
"It just goes to a blank page when i have it go to
Code:
die(mysql_error());"


Try this:
[code]// Open database...
$db = mysql_connect("$localhost", "$databaseuser", "$databasepasswd") or die(mysql_error());
mysql_select_db("$databasename",$db) or die(mysql_error());


// Check and Update affiliate data...
$sql="SELECT userName FROM user WHERE userName='$user'";
$result = mysql_query("$sql",$db) or die(mysql_error());
$numrows=mysql_num_rows($result) or die(mysql_error());
if ($numrows != 0) {
  header("Location:error2.html");
  exit;
}[/code]
Link to comment
Share on other sites

ahh... or die(mysql_error()) should be posted after mysql fuctions.

Use the code I posted a couple of posts ago.

EDIT:
Here it is:
[code]// Open database...
$db = mysql_connect("$localhost", "$databaseuser", "$databasepasswd") or die(mysql_error());
mysql_select_db("$databasename",$db) or die(mysql_error());


// Check and Update affiliate data...
$sql="SELECT userName FROM user WHERE userName='$user'";
$result = mysql_query("$sql",$db) or die(mysql_error());
$numrows=mysql_num_rows($result) or die(mysql_error());
if ($numrows != 0) {
  header("Location:error2.html");
  exit;
}[/code]

[quote]
[quote]Thorpe, surely you need to define $result to use it in an if statement?[/quote]
Why would you? $result = mysql_query($sql,$db) is an expression, expressions return true or false.[/quote]

Isn't = not a comparison expression.  If you want it to return TRUE or FALSE, then wouldn't you have to define $result and use $result == mysql_query($sql,$db)?  $result = mysql_query($sql,$db) makes $result equal to the query, so it will always equal.
Link to comment
Share on other sites

[quote]$result = mysql_query($sql,$db) makes $result equal to the query, so it will always equal[/quote]

Yes, and the query will return true or false.

You must be getting a row returned, have you tried this yet?

[code=php:0]
$sql = "SELECT userName FROM user WHERE userName='$user'";
if ($result = mysql_query($sql,$db)) {
  if (mysql_num_rows($result) > 0) {
    header("Location: error2.html");
    exit;
  }
}
[/code]
Link to comment
Share on other sites

How do I echo mysqlnumrows correcty?

It does connect to the db because if i comment out the whole check part like :
[code]
// Check and Update affiliate data...
//$sql="SELECT userName FROM user WHERE userName='$user'";
//$result = mysql_query("$sql",$db) or die(mysql_error());
//$numrows=mysql_num_rows($result) or die(mysql_error());
//if ($numrows != 0) {
// header("Location:error2.html");
// exit;
}[/code]

Then it updates to the database correctly.

But I need the checking to work because I cannot have duplicate usernames.
Link to comment
Share on other sites

Its weird because i have 3 people on that table and 1 has a username. The others do not.

If there are blank usernames do you think this is why its causeing the error of 2?

Because 2 out of the 3 people in the table have blank usernames?

If so ill have to set a default name so when people fill in the rest of there data (real username) they wont have a problem.

Does this sound good?
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.