spudly1987 Posted December 31, 2014 Share Posted December 31, 2014 I have the following code created, however when i run it it doesn't come back as saying connected successfully? <?php $host = "localhost"; $my_user = "root"; $my_password = ""; $my_db = "notes_db" $con = mysqli_connect("localhost","my_user","my_password","my_db"); echo ("Connected Successfully"); ?> Quote Link to comment Share on other sites More sharing options...
requinix Posted December 31, 2014 Share Posted December 31, 2014 Find your php.ini and change a couple settings within: error_reporting = -1 display_errors = onThen restart Apache (or IIS or whatever) and try your script again. See any error messages? Quote Link to comment Share on other sites More sharing options...
spudly1987 Posted December 31, 2014 Author Share Posted December 31, 2014 Okay i found the php.ini file and i edited and saved the settings, however i am still not receiving the connected succesfully and also not seeing any errors Quote Link to comment Share on other sites More sharing options...
requinix Posted December 31, 2014 Share Posted December 31, 2014 Alright, let's backtrack some. Are you sure you have PHP installed properly? Naming your files with a .php extension? If you do a View Source of the page, do you see your PHP code? Quote Link to comment Share on other sites More sharing options...
mikosiko Posted December 31, 2014 Share Posted December 31, 2014 (edited) After modifying the php.ini did you restarted the Web server? (Apache or whatever) You don't notice anything missing in this line? $my_db = "notes_db" And in this one.. Is that exactly what you wrote using string literals or your were trying to use the previously defined variables? (like $my_host or $my_user, etc.) $con = mysqli_connect("localhost","my_user","my_password","my_db"); Edited December 31, 2014 by mikosiko Quote Link to comment Share on other sites More sharing options...
spudly1987 Posted January 1, 2015 Author Share Posted January 1, 2015 I use xampp and yes after i adjusted the php.ini file i closed out and reopened, and i have corrected the errors, still does not show the "connected succesfully" echo <?php $host = "localhost"; $my_user = "root"; $my_password = ""; $my_db = "notes_db"; $con = mysqli_connect("$host","my_user","my_password","my_db"); echo ("Connected Successfully"); ?> Quote Link to comment Share on other sites More sharing options...
Strider64 Posted January 1, 2015 Share Posted January 1, 2015 (edited) define('DATABASE_HOST', 'localhost'); // normally it's called localhost: define('DATABASE_NAME', 'notes_db'); define('DATABASE_USERNAME', 'root'); // usually it's called root: define('DATABASE_PASSWORD', 'your password'); $con = new mysqli(DATABASE_HOST, DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_NAME); /* check connection */ if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } Edited January 1, 2015 by Strider64 Quote Link to comment Share on other sites More sharing options...
spudly1987 Posted January 1, 2015 Author Share Posted January 1, 2015 How do i know if its connected, all i get is a blank white screen no error Quote Link to comment Share on other sites More sharing options...
requinix Posted January 1, 2015 Share Posted January 1, 2015 Something is not adding up. Either your script will execute the echo and you'll get output, or PHP will stop earlier and issue an error or warning message. I suspect it's the latter. Maybe you don't have mysqli installed. If you're not seeing a message then your php.ini settings are not set correctly. Use phpinfo() to see what your error_reporting and display_errors settings are, and while you're there make sure it mentions mysqli. Quote Link to comment Share on other sites More sharing options...
spudly1987 Posted January 1, 2015 Author Share Posted January 1, 2015 Okay I have worked on some things and now i'm kinda stuck first thing is i got my database connection working great <?php $hostname = "localhost"; $username = "root"; $password = ""; $dbname = "pc_notes"; // Create Connection $con = mysql_connect($hostname, $username, $password, $dbname); mysql_select_db("notes", $con); echo "connected"; ?> when i run it with the url "localhost/notes/db_connection.php, it runs and says connected I also created what is called an insert.php to get the data from the index.html to the database here is the html code <html> <title> PC Note Template </title> <head> </head> <body> <form action="insert.php" method="post"> <label for="">Tech Name: <input type="text" name="techname" /><br><br> <label for="">Date: <input type="date" name="date" /><br><br> <input type="submit" name="submit" value="submit" /> </form> </body> </html> and the below is the insert.php data <?php $sql="INSERT INTO notes (techname, date) VALUES ('$_POST[techname]','$_POST[date]')"; ?> how ever none of the information is being submitted to the database I also noticed that when i run the insert.php by its self i get the following two notices http://localhost/notes/insert.php Notice: Undefined index: techname in C:\xampp\htdocs\notes\insert.php on line 4Notice: Undefined index: date in C:\xampp\htdocs\notes\insert.php on line 4 i did have it working at one point but it was inserting the data blank into the database, so this is where i'm at any help would be great Quote Link to comment Share on other sites More sharing options...
Barand Posted January 1, 2015 Share Posted January 1, 2015 All you are doing in the code in insert.php is defining a string. You have to execute the query defined in that string. Quote Link to comment Share on other sites More sharing options...
spudly1987 Posted January 1, 2015 Author Share Posted January 1, 2015 I have updated my insert.php code <?php $selcted = mysql_select_db("notes"); $mysql ="INSERT INTO notes (techname, date) VALUES ('$_POST[techname]','$_POST[date]')"; if(!mysql_query($mysql)) die(mysql_error()); echo "data inserted"; mysql_close(notes); ?> and now i am getting the following errors Notice: Undefined index: techname in C:\xampp\htdocs\notes\insert.php on line 6Notice: Undefined index: date in C:\xampp\htdocs\notes\insert.php on line 6No database selected Quote Link to comment Share on other sites More sharing options...
Barand Posted January 1, 2015 Share Posted January 1, 2015 You need to to connect to the server before you can select a database. You also need to send the POST data from the form. Check that data has been posted before trying to use the posted data ( isset() ) Quote Link to comment Share on other sites More sharing options...
spudly1987 Posted January 1, 2015 Author Share Posted January 1, 2015 okay i think i have it set correctly $hostname = "localhost"; $username = "root"; $password = ""; $dbname = "pc_notes"; // Create Connection $con = mysql_connect($hostname, $username, $password, $dbname); mysql_select_db("notes", $con); $mysql ="INSERT INTO notes (techname, date) VALUES ('$_POST[techname]','$_POST[date]')"; Quote Link to comment Share on other sites More sharing options...
spudly1987 Posted January 1, 2015 Author Share Posted January 1, 2015 <?php $hostname = "localhost"; $username = "root"; $password = ""; $dbname = "pc_notes"; // Create Connection $con = mysql_connect($hostname, $username, $password, $dbname); mysql_select_db("notes", $con); $mysql ="INSERT INTO notes (techname, date) VALUES ('$_POST[techname]','$_POST[date]')"; $var = ''; if (isset($var)) { } $a = "techname"; $b = "date"; var_dump(isset($a)); var_dump(isset($a, $b)); unset ($a); var_dump(isset($a)); var_dump(isset($a, $b)); $foo = NULL; var_dump(isset($foo)); ?> okay i set this up and i get the results of bool(true) bool(true) bool(false) bool(false) bool(false) Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted January 1, 2015 Share Posted January 1, 2015 Use mysqli, I saw you had both in these posts. Quote Link to comment Share on other sites More sharing options...
spudly1987 Posted January 1, 2015 Author Share Posted January 1, 2015 (edited) i've tried that but every time i switch to mysqli everything breaks on me again <?php $hostname = "localhost"; $username = "root"; $password = ""; $dbname = "pc_notes"; // Create Connection $dbhandle = mysqli_connect($hostname, $username, $password, $dbname); mysqli_select_db("notes", $dbhandle); $mysqli ="INSERT INTO notes (techname, date) VALUES ('$_POST[techname]','$_POST[date]')"; $var = ''; if (isset($var)) { } $a = "techname"; $b = "date"; var_dump(isset($a)); var_dump(isset($a, $b)); unset ($a); var_dump(isset($a)); var_dump(isset($a, $b)); $foo = NULL; var_dump(isset($foo)); ?> Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in C:\xampp\htdocs\notes\db_connection.php on line 9Notice: Undefined index: techname in C:\xampp\htdocs\notes\db_connection.php on line 12Notice: Undefined index: date in C:\xampp\htdocs\notes\db_connection.php on line 12bool(true) bool(true) bool(false) bool(false) bool(false) Edited January 1, 2015 by spudly1987 Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted January 1, 2015 Share Posted January 1, 2015 (edited) connection goes first mysqli mysqli_select_db($dbhandle,"notes"); then need to run a query if ($result = mysqli_query($dbhandle, $mysqli)) { echo "inserted"; } As for the post variables, check them first before even perform any mysql queries is database name pc_notes or notes? The mysqli connection already sets the database unless need to change to something different complete code if(isset($_POST['techname']) && trim($_POST['techname']) != ''){ $techname = trim($_POST['techname']; } if(isset($_POST['date']) && trim($_POST['date']) != ''){ $date = trim($_POST['date']; } if(isset($techname) && isset($date)){ $hostname = "localhost"; $username = "root"; $password = ""; $dbname = "pc_notes"; $dbhandle = mysqli_connect($hostname, $username, $password, $dbname); //if need to change database different than in the connection //mysqli_select_db($dbhandle,"notes"); $mysqli ="INSERT INTO notes (techname, date) VALUES ('$techname','$date')"; if ($result = mysqli_query($dbhandle, $mysqli)) { echo "inserted"; } mysqli_close($dbhandle); } Next would be checking data types and escaping the values before inserting mysqli_real_escape_string Edited January 1, 2015 by QuickOldCar 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.