acarder488 Posted January 10, 2013 Share Posted January 10, 2013 I get this message and not sure how to fix it...... Success... NULL Warning: mysqli_get_host_info() expects parameter 1 to be mysqli, null given in /www/zzl.org/o/t/t/ottawaglandorfems/htdocs/class-db.php on line 14 Success... NULL This is my form - <?php require_once('class-db.php'); /** * function to check the validity of the given string * $what = what you are checking (phone, email, etc) * $data = the string you want to check */ function isValid( $what, $data ) { switch( $what ) { // validate a phone number case 'phone': $pattern = "/^([1]-)?[0-9]{3}-[0-9]{3}-[0-9]{4}$/i"; break; // validate email address case 'email': $pattern = "/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$/i"; break; default: return false; break; } return preg_match($pattern, $data) ? true : false; } $errors = array(); if( isset($_POST['btn_submit']) ) { if( !isValid( 'phone', $_POST['phone'] ) ) { $errors[] = 'Please enter a valid phone number'; } if( !isValid( 'email', $_POST['email'] ) ) { $errors[] = 'Please enter a valid email address'; } } if( !empty($errors) ) { foreach( $errors as $e ) echo "$e <br />"; } if ( !empty ( $_POST ) ) { $type = 'post'; $insert = insert($type, $values); } ?> <html> <head> <title>Registration form for Octoberfest With O-G EMS 2013</title> </head> <body> <form action="" method="post"> <p Registration /> <p>First Name:<input type="text" name="fname" > </p> <p>Last Name:<input type="text" name="lname" > </p> <p>Phone Number:<input type="text" name="phone" ></p> <p>Address:<input type="text" name="address" ></p> <p>Department:<input type="text" name="department" ></p> <p>Email Address:<input type="text" name="email" ></p> <p>Level of Training:<input type="text" name="level" ></p> <input type="submit" name="btn_submit" value="Register"> </form> </body> </html> and this is my script <?php require_once('config1.php'); function connect() { $sql = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME); if ($sql->connect_error) { die('Connect Error (' . $sql->connect_errno . ') ' . $sql->connect_error); }echo 'Success... ' . mysqli_get_host_info($link) . "\n"; return $sql; } function insert($type){ $sql=connect(); /* Set our params */ $fname = $_POST["fname"]; $lname = $_POST["lname"]; $phone = $_POST["phone"]; $address = $_POST["address"]; $department = $_POST["department"]; $email = $_POST["email"]; $level = $_POST["level"]; $query=("INSERT INTO Oktoberfest2013 (First_Name, Last_Name, Phone, Address, Department, Email_Address, Level_of_Traning) values (?, ?, ?, ?, ?, ?, ?)"); /* Create the prepared statement */ if ($stmt = $sql->prepare($query)); var_dump($qSelect); if ( !$sql->query($query) ) { return "Errormessage: $mysqli->error"; /* Bind our params */ $stmt->bind_param('sssssss', $fname, $lname, $phone, $address, $department, $email, $level); /* Execute the prepared Statement */ $stmt->execute(); } } function select($query) { $sql = connect(); $result = $sql->query($query); while ( $obj = $result->fetch_object() ) { $return[] = $obj; } } ?> Thanks in advance for the help. Quote Link to comment Share on other sites More sharing options...
cpd Posted January 10, 2013 Share Posted January 10, 2013 (edited) Look at your connect() function and tell me where you've defined $link... Then take a look at http://php.net/mysqli_get_host_info/ and see what the parameter type it expects. Your select function also returns nothing. Edited January 10, 2013 by cpd Quote Link to comment Share on other sites More sharing options...
acarder488 Posted January 10, 2013 Author Share Posted January 10, 2013 I am receiving this message....Success... mysql.hosting.zymic.com via TCP/IP NULL and not receiving anything in my database. I am not sure what I am doing wrong. Here is my script code <?php require_once('config1.php'); function connect() { $sql = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME); if ($sql->connect_error) { die('Connect Error (' . $sql->connect_errno . ') ' . $sql->connect_error); }echo 'Success... ' . mysqli_get_host_info($sql) . "\n"; return $sql; } function insert($_POST){ $sql=connect(); /* Set our params */ $fname = $_POST["fname"]; $lname = $_POST["lname"]; $phone = $_POST["phone"]; $address = $_POST["address"]; $department = $_POST["department"]; $email = $_POST["email"]; $level = $_POST["level"]; $query=("INSERT INTO Oktoberfest2013 (First_Name, Last_Name, Phone, Address, Department, Email_Address, Level_of_Traning) values (?, ?, ?, ?, ?, ?, ?)"); /* Create the prepared statement */ if ($stmt = $sql->prepare($query)); var_dump($qSelect); if ( !$sql->query($query) ) { return "Errormessage: $mysqli->error"; /* Bind our params */ $stmt->bind_param('sssssss', $fname, $lname, $phone, $address, $department, $email, $level); /* Execute the prepared Statement */ $stmt->execute(); } } function select($query) { $sql = connect(); $result = $sql->query($query); while ( $obj = $result->fetch_object() ) { $return[] = $obj; } } ?> and here is my form code <?php require_once('class-db.php'); /** * function to check the validity of the given string * $what = what you are checking (phone, email, etc) * $data = the string you want to check */ function isValid( $what, $data ) { switch( $what ) { // validate a phone number case 'phone': $pattern = "/^([1]-)?[0-9]{3}-[0-9]{3}-[0-9]{4}$/i"; break; // validate email address case 'email': $pattern = "/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$/i"; break; default: return false; break; } return preg_match($pattern, $data) ? true : false; } $errors = array(); if( isset($_POST['btn_submit']) ) { if( !isValid( 'phone', $_POST['phone'] ) ) { $errors[] = 'Please enter a valid phone number'; } if( !isValid( 'email', $_POST['email'] ) ) { $errors[] = 'Please enter a valid email address'; } } if( !empty($errors) ) { foreach( $errors as $e ) echo "$e <br />"; } if ( !empty ( $_POST ) ) { $type = 'post'; $insert = insert($type, $values); } ?> <html> <head> <title>Registration form for Octoberfest With O-G EMS 2013</title> </head> <body> <form action="" method="post"> <p Registration /> <p>First Name:<input type="text" name="fname" > </p> <p>Last Name:<input type="text" name="lname" > </p> <p>Phone Number:<input type="text" name="phone" ></p> <p>Address:<input type="text" name="address" ></p> <p>Department:<input type="text" name="department" ></p> <p>Email Address:<input type="text" name="email" ></p> <p>Level of Training:<input type="text" name="level" ></p> <input type="submit" name="btn_submit" value="Register"> </form> </body> </html> Thank you for any help...... Quote Link to comment Share on other sites More sharing options...
acarder488 Posted January 10, 2013 Author Share Posted January 10, 2013 if ($sql->connect_error) { die('Connect Error (' . $sql->connect_errno . ') ' . $sql->connect_error); }echo 'Success... ' . mysqli_get_host_info($link) . "\n"; return $sql; } If I make it !sql I get connect error (0) Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted January 10, 2013 Share Posted January 10, 2013 I merged your two topics together. Just because you fixed one error in your code and the symptom/error changed, it's still the same code. Stick to one thread for the same code, so that the history of how you arrived at any point isn't spread out in multiple threads. And while it is true your code is using a mysql database, the issues are in your php logic/variables and nothing specific to mysql (yet), so moving thread to the php help forum... Quote Link to comment Share on other sites More sharing options...
cpd Posted January 10, 2013 Share Posted January 10, 2013 You realise that message is going to be displayed EVERY time you call connect()... Quote Link to comment Share on other sites More sharing options...
acarder488 Posted January 10, 2013 Author Share Posted January 10, 2013 You realise that message is going to be displayed EVERY time you call connect()... Ok so are you tell me that that is okay and normal or are you asking me if I know that until I take that part off it will continue to be present at the to of my form page? If its te second I know I am trying to fix my error and find out what I need to do to fix it. Quote Link to comment Share on other sites More sharing options...
acarder488 Posted January 10, 2013 Author Share Posted January 10, 2013 Thanks for merging that for me and putting it in the right place. I will remember that. Quote Link to comment Share on other sites More sharing options...
cpd Posted January 10, 2013 Share Posted January 10, 2013 What error? You're now getting a message that gives you the host information... Quote Link to comment Share on other sites More sharing options...
acarder488 Posted January 10, 2013 Author Share Posted January 10, 2013 Oh ok I am not receiving any of my data in MySQL database. Thank you. Do you have any idea why that might be? But thanks again for the help on this issue. Quote Link to comment Share on other sites More sharing options...
cpd Posted January 10, 2013 Share Posted January 10, 2013 I can't see anywhere you're trying to retrieve information from your database. Quote Link to comment Share on other sites More sharing options...
acarder488 Posted January 10, 2013 Author Share Posted January 10, 2013 Ok but I it's not inserting anything either and that is more of my issue at this point. Quote Link to comment Share on other sites More sharing options...
cpd Posted January 10, 2013 Share Posted January 10, 2013 (edited) What debugging steps have you taken so far because I see many places something could go wrong... You also have very inconsistent code switching between the procedural MySQLi functions and the object-oriented methods; choose one and stick to it. Edited January 10, 2013 by cpd Quote Link to comment Share on other sites More sharing options...
acarder488 Posted January 10, 2013 Author Share Posted January 10, 2013 No much as of yet new to php and learning. Quote Link to comment Share on other sites More sharing options...
cpd Posted January 10, 2013 Share Posted January 10, 2013 Debugging your SQL Quote Link to comment Share on other sites More sharing options...
acarder488 Posted January 11, 2013 Author Share Posted January 11, 2013 Cpd I went back to something that I started with and I am still having trouble with. it is much shorter script. By the way I like your quote...... <?php require_once('config1.php'); function connect() { $sql = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME); if ($sql->connect_error) { die('Connect Error (' . $sql->connect_errno . ') ' . $sql->connect_error); } return $sql; } function insert($insert){ $sql=connect(); if ( $type == 'post' ) { $query = "INSERT INTO Oktoberfest2013 (First_Name, Last_Name, Phone, Address, Department, Email_Address, Level_of_Training ) VALUES ('$fname', '$lname', '$phone', '$address', '$department, '$email, '$level')"; if ( !$mysqli->query($query) ); }} ?> Here again is my form information..... <?php require_once('class-db.php'); /** * function to check the validity of the given string * $what = what you are checking (phone, email, etc) * $data = the string you want to check */ function isValid( $what, $data ) { switch( $what ) { // validate a phone number case 'phone': $pattern = "/^([1]-)?[0-9]{3}-[0-9]{3}-[0-9]{4}$/i"; break; // validate email address case 'email': $pattern = "/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$/i"; break; default: return false; break; } return preg_match($pattern, $data) ? true : false; } $errors = array(); if( isset($_POST['btn_submit']) ) { if( !isValid( 'phone', $_POST['phone'] ) ) { $errors[] = 'Please enter a valid phone number'; } if( !isValid( 'email', $_POST['email'] ) ) { $errors[] = 'Please enter a valid email address'; } } if( !empty($errors) ) { foreach( $errors as $e ) echo "$e <br />"; } if ( !empty ( $_POST ) ) { $type = 'post'; $insert = insert($type); } ?> <html> <head> <title>Registration form for Octoberfest With O-G EMS 2013</title> </head> <body> <form action="" method="post"> <p Registration /> <p>First Name:<input type="text" name="fname" > </p> <p>Last Name:<input type="text" name="lname" > </p> <p>Phone Number:<input type="text" name="phone" ></p> <p>Address:<input type="text" name="address" ></p> <p>Department:<input type="text" name="department" ></p> <p>Email Address:<input type="text" name="email" ></p> <p>Level of Training:<input type="text" name="level" ></p> <input type="submit" name="btn_submit" value="Register"> </form> </body> </html> Thanks again If you wanted me to do something different please let me know. Quote Link to comment Share on other sites More sharing options...
cpd Posted January 11, 2013 Share Posted January 11, 2013 Your insert() function uses a variable named $type but doesn't define $type anywhere. You then use an if statement after setting the $query variable that has no definition rendering it pointless. By the look of it you need to research "variable scope" because it appears as though you're not sure what it is. Quote Link to comment Share on other sites More sharing options...
acarder488 Posted January 11, 2013 Author Share Posted January 11, 2013 also if I remove the "$" from the front of the values I get this error....Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /www/zzl.org/o/t/t/ottawaglandorfems/htdocs/class-db.php on line 22 Line 22 is $query = "INSERT INTO Oktoberfest2013 (First_Name, Last_Name, Phone, Address, Department, Email_Address, Level_of_Training ) VALUES ('fname', 'lname', 'phone', 'address', 'department, 'email', 'level')"; Quote Link to comment Share on other sites More sharing options...
Christian F. Posted January 11, 2013 Share Posted January 11, 2013 (edited) Instead of using a RegExp pattern to validate the e-mail, you should be using the filter_val () function with the VALIDATE_EMAIL flag. As for your SQL insertion, there are a few things I'd do differently: Don't connect to the database inside the query function. This will only add a lot of overhead, as you'll be making an new DB connection for each query you execute. Instead connect to the database outside of the function, and send the connection as a parameter. The $type variable is not defined inside the function, which means it'll always be evaluated to "false". Don't quite see why you've opted to put it there in the first place though. You're still not checking for SQL errors. As for the quote in his signature: There is some really valuable knowledge in it, the same knowledge that my signature is based upon. The simpler you can make your code, the easier will it be to make it work. However, writing simple code means that you have to understand the problem domain properly. If you don't understand the problem in its entirety, you cannot break it down into the smallest possible subset of steps, which will prevent you from extracting only the necessary steps to accomplish the task at hand. So, writing simple code is hard, while writing complex code is easy. That's why you need to plan before you start to write code. Edited January 11, 2013 by Christian F. Quote Link to comment Share on other sites More sharing options...
acarder488 Posted January 11, 2013 Author Share Posted January 11, 2013 Ok thanks will do. Quote Link to comment Share on other sites More sharing options...
acarder488 Posted January 11, 2013 Author Share Posted January 11, 2013 Thanks Christian, I have been doing html for a while now and was always interested in php but never really has a reason to use it. I am learning slowly but surely that what you say is very true. Thanks for the help and I am sure I will have more questions. I am glad that there are people like you guys who are willing to help. Quote Link to comment Share on other sites More sharing options...
Christian F. Posted January 11, 2013 Share Posted January 11, 2013 You're most welcome, glad to know that we could help. Quote Link to comment Share on other sites More sharing options...
acarder488 Posted January 14, 2013 Author Share Posted January 14, 2013 (edited) Ok Christian I attempted to change my code a bit and see if it helps and I am still having issues with connecting to my database. <?php include_once('init.php'); if (isset($_POST['fname'], $_POST['lname'], $_POST['phone'], $_POST['address'], $_POST['department'], $_POST['email'], $_POST['level'])) { $fname = mysqli_real_escape_string (htmlentities ($_POST['fname'])); $lname = mysqli_real_escape_string (htmlentities ($_POST['lname'])); $address = mysqli_real_escape_string (htmlentities ($_POST['address'])); $department = mysqli_real_escape_string (htmlentities ($_POST['departmenet'])); $email = mysqli_real_escape_string (htmlentities ($_POST['email'])); $level = mysqli_real_escape_string (htmlentities ($_POST['level'])); $query ="INSERT INTO Oktoberfest2013 SET 'First_Name'='{$fname}'; 'Last_Name'='{$lname}'; 'Phone'='{$phone}'; 'Address'='{$address}'; 'Department'='{$department}'; 'Email_Address'='{$email}'; 'Level_of_Training'='{$level}'"; mysqli_query($query); } ?> <html> <head> <title>Registration form for Octoberfest With O-G EMS 2013</title> </head> <body> <form action="" method="post"> <p Registration /> <p>First Name:<input type="text" name="fname" > </p> <p>Last Name:<input type="text" name="lname" > </p> <p>Phone Number:<input type="text" name="phone" ></p> <p>Address:<input type="text" name="address" ></p> <p>Department:<input type="text" name="department" ></p> <p>Email Address:<input type="text" name="email" ></p> <p>Level of Training:<input type="text" name="level" ></p> <input type="submit" name="btn_submit" value="Register"> </form> </body> </html> This is now my form information and here is a init.php <?php include_once('config1.php'); mysqli_connect(DB_HOST, DB_USER, DB_PASS); mysqli_select_db(DB_NAME); ?> I will not post my config1.php being that it has my personal information on it. But it is this type of format.... <?php $config['DB_HOST'] = 'localhost'; $config['DB_USER']= ''; $config['DB_PASS']=""; $config['DB_NAME']= ''; ?> Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'DB_USER'@'192.168.1.1' (using password: YES) in /www/zzl.org/o/t/t/ottawaglandorfems/htdocs/init.php on line 4 Warning: mysql_select_db() [function.mysql-select-db]: Access denied for user 'vhostswww'@'192.168.1.1' (using password: NO) in /www/zzl.org/o/t/t/ottawaglandorfems/htdocs/init.php on line 6 Warning: mysql_select_db() [function.mysql-select-db]: A link to the server could not be established in /www/zzl.org/o/t/t/ottawaglandorfems/htdocs/init.php on line 6 Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given in /www/zzl.org/o/t/t/ottawaglandorfems/htdocs/reg.php on line 13 Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given in /www/zzl.org/o/t/t/ottawaglandorfems/htdocs/reg.php on line 14 Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given in /www/zzl.org/o/t/t/ottawaglandorfems/htdocs/reg.php on line 15 Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given in /www/zzl.org/o/t/t/ottawaglandorfems/htdocs/reg.php on line 16 Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given in /www/zzl.org/o/t/t/ottawaglandorfems/htdocs/reg.php on line 17 Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given in /www/zzl.org/o/t/t/ottawaglandorfems/htdocs/reg.php on line 18 Warning: mysqli_query() expects at least 2 parameters, 1 given in /www/zzl.org/o/t/t/ottawaglandorfems/htdocs/reg.php on line 32 Any suggestions? Edited January 14, 2013 by acarder488 Quote Link to comment Share on other sites More sharing options...
cpd Posted January 14, 2013 Share Posted January 14, 2013 You're trying to access the variables as if they are constants. E.g. define("DB_USER", "MyDatabaseUsername"); When you've actually defined the configuration as an array. To use your current set-up you should do: mysqli_connect($config['DB_HOST'], $config['DB_USER'], $config['DB_PASS']); Quote Link to comment Share on other sites More sharing options...
acarder488 Posted January 14, 2013 Author Share Posted January 14, 2013 Thanks CPD. Thanks seemed to fix the escape issues but I sill get Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'DB_USER'@'192.168.1.1' (using password: YES) in/www/zzl.org/o/t/t/ottawaglandorfems/htdocs/init.php on line 4 Warning: mysql_select_db() [function.mysql-select-db]: Access denied for user 'vhostswww'@'192.168.1.1' (using password: NO) in/www/zzl.org/o/t/t/ottawaglandorfems/htdocs/init.php on line 6 Warning: mysql_select_db() [function.mysql-select-db]: A link to the server could not be established in/www/zzl.org/o/t/t/ottawaglandorfems/htdocs/init.php on line 6 These three errors. 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.