You're working on what's supposed to be a MySQL resource BEFORE you perform the query. $obj1 isn't anything when you're working with it. It's a blank variable (unless that stuff happens in connect-sql.php, but I'm going to guess it doesn't). You put the query into a string and then start working with rows that you never asked for. After that, you perform the query with a different library (the mysql library and mysqli library are different; I'm not sure if they're interchangeable).
Also, sanitize your input. Run mysql_real_escape_string() on your $username variable before inserting it into that query. If you don't, you open yourself to SQL injection attacks, and that's not something you want.
Okay, I've just changed the code alot here's what I changed.
Also I still haven't added the escape string in yet as I'm not sure of how to write it. Like this?
mysql_real_escape_string($username);
Here's the new code.
<?php
if (isset($_POST['submitted'])) {
include('connect-mysql.php');
$username = $_POST['username'];
$query = mysqli_query($dbcon, "SELECT * FROM data WHERE username = '$username'");
$num = mysql_num_rows($query);
$newrecord = "1 record added to the database";
if($num) {
// Name exsists
} else {
// Add
if (!mysqli_query($dbcon, "INSERT INTO data (username) VALUES ('$username')")) {
die('error inserting new record');
} //end of nested if statement
}
} //end of main if statement
?>
Please help me I only get one error now.
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/wvvwme/public_html/beta.wvvw.me/insert-data.php on line 10
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/wvvwme/public_html/beta.wvvw.me/insert-data.php on line 10
Use mysql_error() to see what the problem with your query is. The fact that it says the supplied argument is not a valid MySQL result resource means that the query didn't complete successfully.
Edit: Umm... you're using the mysqli library and the mysql library interchangably. MySQLi resources work with functions starting with mysqli and mysql functions are prefixed with mysql. You can't use mysqli_query() and then mysql_num_rows() on that resource. Change mysqli_query to mysql_query and you should be okay.
"If you aren't ashamed of what you coded 6 months ago, you aren't progressing as much as you should." - My Website : Altar-Apps,Applications, Libraries, APIs, Code snippets and the Heart of Sin roguelike game!
I'm not entirely sure (i haven't work with php/mysql in a long time), but try removing the ' ' from around $username :
INSERT INTO data (username) VALUES ($username)
...Worth a try? :tongue.gif:
EDIT : Same thing for the SELECT instruction.
No, the problem is that he's using the mysql and mysqli libraries interchangeably. Removing the single quotes from the query will make it invalid. It'll yell at him about invalid column names.
No, the problem is that he's using the mysql and mysqli libraries interchangeably. Removing the single quotes from the query will make it invalid. It'll yell at him about invalid column names.
I took what you guys said into consideration.
Here's the code now :smile.gif:
<?php
if (isset($_POST['submitted'])) {
include('connect-mysql.php');
$username = $_POST['username'];
mysql_select_db("data", $dbcon);
$query = mysql_query("SELECT * FROM data WHERE username = '$username'", $dbcon) or die(mysql_error());
$num = mysql_num_rows($query);
$newrecord = "1 record added to the database";
if($num) {
// Name exsists
} else {
// Add
if (!mysql_query($dbcon, "INSERT INTO data (username) VALUES ('$username')")) {
die('error inserting new record');
} //end of nested if statement
}
} //end of main if statement
?>
Though I'm still getting this.
Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in /home/wvvwme/public_html/beta.wvvw.me/insert-data.php on line 8
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/wvvwme/public_html/beta.wvvw.me/insert-data.php on line 10
Is "data" the name of a database or is it just the name of a table?
I think the table.
Here's my phpmyadmin.
I changed it to wvvw_beta.
<?php
if (isset($_POST['submitted'])) {
include('connect-mysql.php');
$username = $_POST['username'];
mysql_select_db('wvvwme_beta', $dbcon);
$query = mysql_query("SELECT * FROM data WHERE username = '$username'", $dbcon) or die(mysql_error());
$num = mysql_num_rows($query);
$newrecord = "1 record added to the database";
if($num) {
// Name exsists
} else {
// Add
if (!mysql_query($dbcon, "INSERT INTO data (username) VALUES ('$username')")) {
die('error inserting new record');
} //end of nested if statement
}
} //end of main if statement
?>
<?php
if (isset($_POST['submitted'])) {
include('connect-mysql.php');
mysql_connect('localhost', 'wvvwme_beta', '*Password*', 'wvvwme_beta');
mysql_select_db('_beta', $dbcon);
$username = $_POST['username'];
$query = mysql_query("SELECT * FROM data WHERE username = '$username'", $dbcon) or die(mysql_error());
$num = mysql_num_rows($query);
$newrecord = "1 record added to the database";
if($num) {
// Name exsists
} else {
// Add
if (!mysql_query($dbcon, "INSERT INTO data (username) VALUES ('$username')")) {
die('error inserting new record');
} //end of nested if statement
}
} //end of main if statement
?>
Password's not real one though.
Now all im getting is a blank page with no errors or it writing to the database.
This is my connect-mysql.php with out the real password.
The Meaning of Life, the Universe, and Everything.
Join Date:
4/23/2011
Posts:
42
Member Details
Try this:
<?php
if (isset($_POST['submitted'])) {
mysql_connect('localhost', 'wvvwme_beta', '*Password*');
mysql_select_db('_beta');
$username = $_POST['username'];
$query = mysql_query("SELECT * FROM data WHERE username = '$username'") or die(mysql_error());
$num = mysql_num_rows($query);
$newrecord = "1 record added to the database";
if($num) {
// Name exsists
} else {
// Add
if (!mysql_query($dbcon, "INSERT INTO data (username) VALUES ('$username')")) {
die('error inserting new record');
} //end of nested if statement
}
} //end of main if statement
?>
<?php
if (isset($_POST['submitted'])) {
mysql_connect('localhost', 'wvvwme_beta', '*Password*');
mysql_select_db('_beta');
$username = $_POST['username'];
$query = mysql_query("SELECT * FROM data WHERE username = '$username'") or die(mysql_error());
$num = mysql_num_rows($query);
$newrecord = "1 record added to the database";
if($num) {
// Name exsists
} else {
// Add
if (!mysql_query($dbcon, "INSERT INTO data (username) VALUES ('$username')")) {
die('error inserting new record');
} //end of nested if statement
}
} //end of main if statement
?>
I updated the OP with the new code and changed the start part to the one posted above.
Now I get No database selected. :sad.gif:
Please don't delete this thread as I might need it still for more questions as I write more of the page's code.
Here's the URL for people that want to look at my development.
Also, sanitize your input. Run mysql_real_escape_string() on your $username variable before inserting it into that query. If you don't, you open yourself to SQL injection attacks, and that's not something you want.
Okay, I've just changed the code alot here's what I changed.
Also I still haven't added the escape string in yet as I'm not sure of how to write it. Like this?
Here's the new code.
Please help me I only get one error now.
Use mysql_error() to see what the problem with your query is. The fact that it says the supplied argument is not a valid MySQL result resource means that the query didn't complete successfully.
Edit: Umm... you're using the mysqli library and the mysql library interchangably. MySQLi resources work with functions starting with mysqli and mysql functions are prefixed with mysql. You can't use mysqli_query() and then mysql_num_rows() on that resource. Change mysqli_query to mysql_query and you should be okay.
...Worth a try? :tongue.gif:
EDIT : Same thing for the SELECT instruction.
- My Website : Altar-Apps, Applications, Libraries, APIs, Code snippets and the Heart of Sin roguelike game!
No, the problem is that he's using the mysql and mysqli libraries interchangeably. Removing the single quotes from the query will make it invalid. It'll yell at him about invalid column names.
I took what you guys said into consideration.
Here's the code now :smile.gif:
Though I'm still getting this.
:biggrin.gif:
I didn't even think of that, thanks.
Now the only problem I'm getting is No database selected.
Any ideas.
I think the table.
Here's my phpmyadmin.
I changed it to wvvw_beta.
Password's not real one though.
Now all im getting is a blank page with no errors or it writing to the database.
This is my connect-mysql.php with out the real password.
Take out the DB_NAME argument.
also add "or die(mysql_error())" after mysql_connect() and mysql_select_db()
Done, but it's still showing a blank screen and not the error code.
Also it's still not writing to the database.
Switch the SQL and the $dbcon.
That did nothing. :sad.gif:
Also, at the top of your file (right after <?php) could you add "error_reporting(E_ALL);" and see if that displays any errors at all.
I updated the OP with the new code and changed the start part to the one posted above.
Now I get No database selected. :sad.gif:
Here we go again...