the1h3r0 Posted August 12, 2013 Share Posted August 12, 2013 I've seen this error posted everywhere and no one's answers seem to pertain to mine. This is my code: <?php $password=$_POST['password']; $check=$_POST['checkid']; $con=mysqli_connect("db","dbuser","$password","db","3306"); //Gives player a new loadout function playerLoadout() { mysqli_query($con,"INSERT INTO cust_loadout_profile (cust_loadout_id, unique_id) VALUES ('$_POST[loadoutid]','$_POST[uniqueid]')"); } //Updates existing player's loadout function updateLoadout() { mysqli_query($con,"UPDATE cust_loadout_profile SET cust_loadout_id='$_POST[loadoutidchange]' WHERE unique_id='$_POST[uniqueidchange]'"); } //Creates new loadout function createLoadout() { mysqli_query($con,"INSERT INTO cust_loadout (id, inventory, backpack, description) VALUES ('$_POST[id]','$_POST[inventory]','$_POST[backpack]','$_POST[description]')"); } //Deletes selected loadout function deleteLoadout() { mysqli_query($con,"DELETE FROM cust_loadout WHERE id = '$_POST[iddelete]'"); } //Table that shows current loadouts mysql_connect("db","dbuser","password"); mysql_select_db("db"); $data = mysql_query("SELECT * FROM `cust_loadout_profile` WHERE `unique_id` = '$check' ") or die(mysql_error()); while($info = mysql_fetch_array( $data )) { Print "Current Loadout: ".$info['cust_loadout_id'] . " "; } if (!empty($_POST['uniqueid'])) { playerLoadout(); } if (!empty($_POST['uniqueidchange'])) { updateLoadout(); } if (!empty($_POST['id'])) { createLoadout(); } if (!empty($_POST['iddelete'])) { deleteLoadout(); } ?> and the mysqli_query() expects parameter 1 to be mysqli, null given error occurs on line 28 which is this query: mysqli_query($con,"INSERT INTO cust_loadout (id, inventory, backpack, description) VALUES ('$_POST[id]','$_POST[inventory]','$_POST[backpack]','$_POST[description]')"); any ideas what is causing this error? $con is defined at the beginning of the code and it doesn't give me that error for the first two functions, just this third one. Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted August 12, 2013 Share Posted August 12, 2013 (edited) added isset to get rid of the undefined index errors on checkid & password. also used global var scope inside of your function to declare the connection inside of the function. $password=isset($_POST['password']); $check=isset($_POST['checkid']); $con=mysqli_connect("db","dbuser","$password","db","3306"); //Gives player a new loadout function playerLoadout() { global $con; mysqli_query($con,"INSERT INTO cust_loadout_profile (cust_loadout_id, unique_id) VALUES ('$_POST[loadoutid]','$_POST[uniqueid]')"); } //Updates existing player's loadout function updateLoadout() { global $con; mysqli_query($con,"UPDATE cust_loadout_profile SET cust_loadout_id='$_POST[loadoutidchange]' WHERE unique_id='$_POST[uniqueidchange]'"); } //Creates new loadout function createLoadout() { global $con; mysqli_query($con,"INSERT INTO cust_loadout (id, inventory, backpack, description) VALUES ('$_POST[id]','$_POST[inventory]','$_POST[backpack]','$_POST[description]')"); } //Deletes selected loadout function deleteLoadout() { global $con; mysqli_query($con,"DELETE FROM cust_loadout WHERE id = '$_POST[iddelete]'"); } //Table that shows current loadouts mysql_connect("db","dbuser","password"); mysql_select_db("db"); $data = mysql_query("SELECT * FROM `cust_loadout_profile` WHERE `unique_id` = '$check' ") or die(mysql_error()); while($info = mysql_fetch_array( $data )) { Print "Current Loadout: ".$info['cust_loadout_id'] . " "; } if (!empty($_POST['uniqueid'])) { playerLoadout(); } if (!empty($_POST['uniqueidchange'])) { updateLoadout(); } if (!empty($_POST['id'])) { createLoadout(); } if (!empty($_POST['iddelete'])) { deleteLoadout(); } Edited August 12, 2013 by darkfreaks Quote Link to comment Share on other sites More sharing options...
the1h3r0 Posted August 12, 2013 Author Share Posted August 12, 2013 added isset to get rid of the undefined index errors on checkid & password. also used global var scope inside of your function to declare the connection inside of the function. $password=isset($_POST['password']); $check=isset($_POST['checkid']); $con=mysqli_connect("db","dbuser","$password","db","3306"); //Gives player a new loadout function playerLoadout() { global $con; mysqli_query($con,"INSERT INTO cust_loadout_profile (cust_loadout_id, unique_id) VALUES ('$_POST[loadoutid]','$_POST[uniqueid]')"); } //Updates existing player's loadout function updateLoadout() { global $con; mysqli_query($con,"UPDATE cust_loadout_profile SET cust_loadout_id='$_POST[loadoutidchange]' WHERE unique_id='$_POST[uniqueidchange]'"); } //Creates new loadout function createLoadout() { global $con; mysqli_query($con,"INSERT INTO cust_loadout (id, inventory, backpack, description) VALUES ('$_POST[id]','$_POST[inventory]','$_POST[backpack]','$_POST[description]')"); } //Deletes selected loadout function deleteLoadout() { global $con; mysqli_query($con,"DELETE FROM cust_loadout WHERE id = '$_POST[iddelete]'"); } //Table that shows current loadouts mysql_connect("db","dbuser","password"); mysql_select_db("db"); $data = mysql_query("SELECT * FROM `cust_loadout_profile` WHERE `unique_id` = '$check' ") or die(mysql_error()); while($info = mysql_fetch_array( $data )) { Print "Current Loadout: ".$info['cust_loadout_id'] . " "; } if (!empty($_POST['uniqueid'])) { playerLoadout(); } if (!empty($_POST['uniqueidchange'])) { updateLoadout(); } if (!empty($_POST['id'])) { createLoadout(); } if (!empty($_POST['iddelete'])) { deleteLoadout(); } I tried that and now I'm getting two errors. mysqli_connect() [function.mysqli-connect]: (28000/1045): Access denied for user 'dbuser'@'db' (using password: YES) and mysqli_query() expects parameter 1 to be mysqli, null again on line 49 this time. Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted August 12, 2013 Share Posted August 12, 2013 does your database have unlimited permissions? Quote Link to comment Share on other sites More sharing options...
the1h3r0 Posted August 12, 2013 Author Share Posted August 12, 2013 does your database have unlimited permissions? is there a way to check this in phpmyadmin? Quote Link to comment Share on other sites More sharing options...
the1h3r0 Posted August 12, 2013 Author Share Posted August 12, 2013 (edited) and it's weird. sometimes i get the access denied error sometimes not. i did have it working earlier tonight but it was so buggy it wasn't worth* it. but i can definitely make successful connections from this host. Edited August 12, 2013 by the1h3r0 Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted August 12, 2013 Share Posted August 12, 2013 create a new user in cpanel MYSQL databases with ALL privileges. Quote Link to comment Share on other sites More sharing options...
the1h3r0 Posted August 12, 2013 Author Share Posted August 12, 2013 create a new user in cpanel MYSQL databases with ALL privileges. it would appear the host doesn't allow access to people creating new users. like i said, i had it working to the point where i could connect to the database with no problems. i just dont know what is causing that mysqli error when $con is obviously defined as a mysqli link when its saying its receiving null. Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted August 12, 2013 Share Posted August 12, 2013 have you checked for errors?/ if (mysqli_connect_error()) { die('Could not connect to the database'); ] Quote Link to comment Share on other sites More sharing options...
the1h3r0 Posted August 12, 2013 Author Share Posted August 12, 2013 have you checked for errors?/ if (mysqli_connect_error()) { die('Could not connect to the database'); ] where do i place this? Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted August 12, 2013 Share Posted August 12, 2013 wherever the $con variable is defined in the code after it. Quote Link to comment Share on other sites More sharing options...
the1h3r0 Posted August 12, 2013 Author Share Posted August 12, 2013 wherever the $con variable is defined in the code after it. It just says Host 'ipgoeshere' is not allowed to connect to this MySQL server. But on the page before this error happens, that same IP *does* connect to the database and it pulls information into a table. So for some reason when I hit submit its not longer allowing access to the server...i think Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted August 12, 2013 Share Posted August 12, 2013 i still think you have not allowed your user name all privileges Quote Link to comment Share on other sites More sharing options...
the1h3r0 Posted August 12, 2013 Author Share Posted August 12, 2013 i still think you have not allowed your user name all privileges like i said, i dont have access to do that. i just find it really strange that i almost had the script working (it connected to the database with no problems whatsoever) and now all the sudden i apparently have no access to the database at all. Quote Link to comment Share on other sites More sharing options...
tanzeelniazi Posted August 12, 2013 Share Posted August 12, 2013 I think the problem is in $con=mysqli_connect("db","dbuser","$password","db","3306"); You are not connecting to any host, your value "db" for host and database can't be same. The correct syntax is: mysqli_connect($host, $db_user, $db_user_password, $database); I haven't seen rest of the code, correct it and then tell if it works or not. Quote Link to comment Share on other sites More sharing options...
PaulRyan Posted August 12, 2013 Share Posted August 12, 2013 I can see quite a few things wrong with this, before any errors you are currently getting. Firstly, do not use GLOBAL, if you have to use globals, you're doing it wrong. Pass the connection to the function instead. Secondly, you do not sanitize any incoming data. You need to make sure all data is safe to use in queries. Thirdly, you should also pass the $_POST variables to the functions after sanitizing it. For some reason you are connecting with MySQLi at the top of the file, then connecting with MySQL later on in the file. Pick one and stick with it, MySQLi is the better option. Quote Link to comment Share on other sites More sharing options...
the1h3r0 Posted August 12, 2013 Author Share Posted August 12, 2013 I can see quite a few things wrong with this, before any errors you are currently getting. Firstly, do not use GLOBAL, if you have to use globals, you're doing it wrong. Pass the connection to the function instead. Secondly, you do not sanitize any incoming data. You need to make sure all data is safe to use in queries. Thirdly, you should also pass the $_POST variables to the functions after sanitizing it. For some reason you are connecting with MySQLi at the top of the file, then connecting with MySQL later on in the file. Pick one and stick with it, MySQLi is the better option. how do i pass the connection into the function? Quote Link to comment Share on other sites More sharing options...
the1h3r0 Posted August 12, 2013 Author Share Posted August 12, 2013 Okay so my connection issue is solved. Now there's another problem. One function I want to add is giving a person a new custom loadout. This is done by connecting to the database, selecting the table, and inserting a row with a cust_loadout_id and a unique_id value which are taken from input forms on the previous page. When i run this function I get no errors at all, but when I check the database, there is no new entry. is it because cust_loadout_id has a primary key which is only a certain amount of values? if so, is there a way that when inputting the cust_loadout_id value there is a dropdown list that shows all the primary key values as opposed to a blank text form? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.