Viewing Database and checking submittion

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Bogey
Forum Commoner
Posts: 34
Joined: Sat Nov 10, 2007 5:51 pm

Viewing Database and checking submittion

Post by Bogey »

Okay... here goes... something?

I created a registration form but at the moment it submits ANYTHING that it gets that passes the validation... what I want is for it to check the database for already existing name and email and if the user submitted info with the same Name and/or Email than the user would get an error.

Please I need some help with this... I can't make the darn mysql select statement to work and that is really frustrating...

For displaying the values... I can't get the following to work :evil: :x

Code: Select all

$result = mysql_fetch_array(mysql_query("SELECT Name FROM `user` WHERE Email='some@email.com'"));
echo $result;
All I get from that is "Resource id #3" and not the values in the columns of the table...

And for checking... I can't get the following to work :x

Code: Select all

$query="SELECT  Name,Email  FROM  `user`  WHERE  Name='$name', Email='$email'";
if(mysql_fetch_array(mysql_query($query)))
{
  echo  "The following values are already on the database:";
  echo $name;
  echo '<br />';
  echo $email;
  echo '<br />';
  echo 'So, either you already have an account or you have someone by your name signed up for membership.<br />';
  echo 'If this is you than please click on "Forgot Password" link at sign-up. <br />';
}
Also doesn't work... I'M GOING MAD WITH THIS... I REALLY NEED HELP WITH THIS... PLEASE. I have no idea whatsoever of what I'm doing wrong...
Bogey
Forum Commoner
Posts: 34
Joined: Sat Nov 10, 2007 5:51 pm

Post by Bogey »

Okay, I can now view EVERYTHING that I have in the database (that I chose to see) with the following code...

Code: Select all

$result = mysql_query("SELECT gender,birthday,secretquestion,secretanswer FROM user") or die(mysql_error());

$tsv  = array();
$html = array();
while($row = mysql_fetch_array($result, MYSQL_NUM))
{
   $tsv[]  = implode("\t", $row);
   $html[] = "<tr><td>" .implode("</td><td>", $row) .              "</td></tr>";
   $htmm = "<br />" . implode("</td><td>", $row) ."<br />";
}

$tsv = implode("\r\n", $tsv);
$html = '<table border="1" width="100%"><tr><td bgcolor="#FFCC00">Gender</td><td bgcolor="#FFCC00">Birthday</td><td bgcolor="#FFCC00">Secret Question</td><td bgcolor="#FFCC00">Secret Answer</td></tr>' . implode("\r\n", $html) . '<br /></table>';



//echo $tsv;
echo $html;
The only problem I have is now checking...
Bogey
Forum Commoner
Posts: 34
Joined: Sat Nov 10, 2007 5:51 pm

Post by Bogey »

By "checking" I mean the following.

I have a registration form.
A user can submit the info into the database through the registration form.
The registration form submits the submitted values without checking the database if they already exist.
I want the script to check the database if the values already exist.
If the values already exist it would bring an error.
If there are no values like the user submitted, then the values would be submitted into the database.

Everything I tried doesn't work... What I believe the fault is, is the actual checking... here is what I have so far... I know that it is not right... I need someone to help me fix it...

Code: Select all

$query2="SELECT name,email FROM user WHERE  Name='$name' OR Email='$email'";
$query="mysql_query($query2)";
if($query == '0')
{
  echo 'Sorry, either the username already taken or the email address is already in our database.';
  echo 'So, either you already have an account or you have someone by your name signed up for membership.<br />';
  echo 'If this is you than please click on "Forgot Password" link at sign-up. <br />';
} else {
//Data insertion
mysql_query("INSERT INTO `user` (`Name`, `Email`, `Password`, `Gender`, `Birthday`, `SecretQuestion`, `SecretAnswer`, `Level`) VALUES ('$name', '$email', '$password', '$gender', '$birthday', '$question', '$answer', 'Admin')")
or die(mysql_error());
//Inserted data (values)
echo '<p>The following values have being added to the Database... </p>';
echo $name;
echo '<br />';
echo $email;
echo '<br />';
echo $password;
echo '<br />';
echo $gender;
echo '<br />';
echo $birthday;
echo '<br />';
echo $question;
echo '<br />';
echo $answer;
echo '<br />';
echo '<p>Thanks for registering on this site!</p>';
}
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Post by califdon »

What you need to do is study the syntax for retrieving data from a database. $result is an entire recordset of data, in your example. So you cannot print it out, you have to fetch one row at a time from the result. Once you have a row, you have to address each column in the row, either as an indexed array element ($row[3]) or a column name, if you have used mysql_fetch_assoc() and used extract($row) to assign columns to variables ($Email).
Bogey
Forum Commoner
Posts: 34
Joined: Sat Nov 10, 2007 5:51 pm

Post by Bogey »

If I say

Code: Select all

($row[3])
that would mean the third row would be selected?
allphpsolutions
Forum Newbie
Posts: 1
Joined: Sun Oct 14, 2007 11:47 am

Post by allphpsolutions »

try,

Code: Select all

if(mysql_num_rows(mysql_query("SELECT * FROM `user` WHERE Name = '$name'") > 0)
  //allready exist
  else
  //insert
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Post by califdon »

Bogey wrote:If I say

Code: Select all

($row[3])
that would mean the third row would be selected?
No. Rather than trying to explain the syntax here, I urge you to just read a beginning tutorial on how to retrieve data from a MySQL database, using PHP. You could start with: http://www.php-mysql-tutorial.com/php-mysql-select.php
Post Reply