cubx Posted December 1, 2006 Share Posted December 1, 2006 Here is the snipped of code I'm using:if (!isset($_POST['Submit'])) form();elseif (!blank_field()) echo "fields left blank"; else if (checklength($_POST[password], 5, 15)) echo "password length incorrect";if (connect_to_db()) { $query = "select username from accounts where username='[username]'"; $result = $mysql_query($query, $link); if ($result) // true = user already exists echo "User already exists"; else echo "You are a new user";}The blank fields and wrong password length are working fine. I am using mysql 5.0.27, php 5.2.0, and apache 2.2.3, if that matters.I created the database and added in 2 users using the command line. I have a form that asks for a username and password, then this part of the code is supposed to check the form username with the mysql username for duplicates.I don't get any errors, even though I am typing in the same name, which should result in "User already exists". All it does is lets me enter the information, and keeps going.Any pointers? Do I need to post the entire php file? Link to comment https://forums.phpfreaks.com/topic/29087-problem-with-mysql_query/ Share on other sites More sharing options...
Flukey Posted December 1, 2006 Share Posted December 1, 2006 [code=php:0] $result = $mysql_query($query, $link) or die(mysql_error()); [/code]See if there is a mysql error. :) Link to comment https://forums.phpfreaks.com/topic/29087-problem-with-mysql_query/#findComment-133319 Share on other sites More sharing options...
cubx Posted December 1, 2006 Author Share Posted December 1, 2006 There are no errors showing. I even added the die statement like you said and tried again. I put in a username which does exist, so I should have receive a duplicate username warning.Nothing. It just proceeds as normal. Obviously something is wrong. Link to comment https://forums.phpfreaks.com/topic/29087-problem-with-mysql_query/#findComment-133327 Share on other sites More sharing options...
HuggieBear Posted December 1, 2006 Share Posted December 1, 2006 Try changing this:[code=php:0]$query = "select username from accounts where username='[username]'";[/code] To this:[code=php:0]$query = "select username from accounts where username='{$_POST['username']}'";[/code] RegardsHuggie Link to comment https://forums.phpfreaks.com/topic/29087-problem-with-mysql_query/#findComment-133329 Share on other sites More sharing options...
cubx Posted December 1, 2006 Author Share Posted December 1, 2006 Ok I did that. Now my code looks like this:if (connect_to_db()) { $query = "select username from accounts where username='{$_POST['username']}'"; $result = $mysql_query($query, $link) or die(mysql_error()); echo "$result";// if ($result) // true = user already exists// echo "User already exists";// else// echo "You are a new user";}I'll remove the comments once I know it's working. Anyway, there are no errors, the page goes blank.I get no results at all. I keep looking over google for examples like this, but can't find anything. What I'm trying to do is have the form pass a username to php, which calls mysql, does a search for the username, and says it is exists. For the life of me, I cannot find any tutorials online where someone else has done this. I can't imagine I'm the first. Link to comment https://forums.phpfreaks.com/topic/29087-problem-with-mysql_query/#findComment-133338 Share on other sites More sharing options...
HuggieBear Posted December 1, 2006 Share Posted December 1, 2006 OK, here's a complete code, assuming that the fields in your form are called username and password and you're using the post method.[code]<?php// Have the username and password been filled outif (!empty($_POST['username']) && !empty($_POST['password'])){ // Query the database $sql = "SELECT username FROM users WHERE username = '{$_POST['username']}'"; // Check if we got a result returned $result = mysql_query($sql); if (!$result){ echo "Couldn't execute: $sql<br><br>\n" . mysql_error(); } else { // Does the user exist, how many rows were retrurned if (mysql_num_rows($result) > 0){ echo "Username already in use\n"; } else { echo "Username doesn't exist\n"; } }}else { // Your code to echo the form goes here}?>[/code]RegardsHuggie Link to comment https://forums.phpfreaks.com/topic/29087-problem-with-mysql_query/#findComment-133349 Share on other sites More sharing options...
pradeep_achean Posted December 1, 2006 Share Posted December 1, 2006 how the future will be in phphow the projects will be coming Link to comment https://forums.phpfreaks.com/topic/29087-problem-with-mysql_query/#findComment-133351 Share on other sites More sharing options...
cubx Posted December 1, 2006 Author Share Posted December 1, 2006 Huggie... I replaced my code with yours. Yours is much cleaner. However, I am still not getting any database feedback. I type in my username on the form, which I manually added to mysql on my linux server, but after I click submit, I get nothing. No errors, not even the echo statement saying if the username exists.Everything is running fine on my linux machine. I just checked it. I can't figure this out.Here is the script:<?php// Have the username and password been filled outfunction form(){ echo "<table width=\"100%\" height=\"5%\" bgcolor=\"cyan\">"; echo "<tr>"; echo "<td bgcolor=\"white\" width=\"20%\"><font color=\"green\"> <font size=+1><b>Step 1</b></font></td>"; echo "<td> <font size=+1 color=\"white\"><b>Step 2</b></font></td>"; echo "<td> <font size=+1 color=\"white\"><b>Step 3</b></font></td>"; echo "<td> <font size=+1 color=\"white\"><b>Step 4</b></font></td>"; echo "<td> <font size=+1 color=\"white\"><b>Step 5</b></font></td>"; echo "</tr>"; echo "<tr bgcolor=\"white\">"; echo "<td bgcolor=\"red\" width=\"20%\"> <b><i><font size=+1>LOG IN</i></b></td>"; echo "<td></td>"; echo "<td></td>"; echo "<td></td>"; echo "<td></td>"; echo "</tr>"; echo "</table>"; echo "<br><br><br><br><br><br><br><br><br><center>"; echo "<table border=\"0\" bgcolor=\"#66ccff\" cellpadding=\"10\">"; echo "<form action='" .$_SERVER['PHP_SELF'] ."' method='post'>"; echo "<tr><td>What username do you want?</td><td><input type='text' name='username'></td>"; echo "<tr><td>Pick a password (5-15 characters):</td><td><input type='password' name='password'></td>"; echo "<tr><td colspan=2 align=center><br><input type='image' name='Submit' value='Submit' src='submit.gif'>"; echo "</form>"; echo "</table></center>";} function connect_to_db() { $link = mysql_connect('localhost', 'user', 'password'); if (!$link) { die('Could not connect: ' . mysql_error()); } $db_selected = mysql_select_db('mydb', $link) or die(mysql_error()); if (!$db_selected) { echo "Cannot use database<br>"; echo "mysql_error()"; }}function blank_field(){ while(list($name,$val)=each($_POST)){ if (!$val) return FALSE; // if fields are blank } return TRUE;}if (!isset($_POST['Submit'])) form();elseif (connect_to_db()) {if (!empty($_POST['username']) && !empty($_POST['password'])){ // Query the database $sql = "SELECT username FROM users WHERE username = '{$_POST['username']}'"; // Check if we got a result returned $result = mysql_query($sql); if (!$result){ echo "Couldn't execute: $sql<br><br>\n" . mysql_error(); } else { // Does the user exist, how many rows were retrurned if (mysql_num_rows($result) > 0){ echo "Username already in use\n"; } else { echo "Username doesn't exist\n"; } echo "here is the form"; } // end else} // end if} // end while?>What could I possibly be missing? I at least expected it to say if the username exists, but it doesn't. Link to comment https://forums.phpfreaks.com/topic/29087-problem-with-mysql_query/#findComment-133391 Share on other sites More sharing options...
HuggieBear Posted December 1, 2006 Share Posted December 1, 2006 Add the following to the top of your code...[code=php:0]error_reporting(E_ALL);[/code] RegardsHuggie Link to comment https://forums.phpfreaks.com/topic/29087-problem-with-mysql_query/#findComment-133395 Share on other sites More sharing options...
cubx Posted December 1, 2006 Author Share Posted December 1, 2006 Ok I got it working. Even the error reporting thing didn't help. What it turned out to be is in the connect_to_db function, after the part where it dies if it fails, I needed an "else" there, and at the end of the function, it wasn't returning a value, which apparently the main loop was expecting. I added the else, return TRUE, and now it works.Thank you! Link to comment https://forums.phpfreaks.com/topic/29087-problem-with-mysql_query/#findComment-133400 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.