k3n3t1k Posted August 29, 2007 Share Posted August 29, 2007 I've been having alot of trouble when trying to Insert data into a MySQL Database from a PHP script. Does anybody know why it wont insert the information? Link to comment Share on other sites More sharing options...
justsomeguy Posted August 29, 2007 Share Posted August 29, 2007 No, no one knows. Perhaps if you gave details about your problem or posted the code you're using we could help. Link to comment Share on other sites More sharing options...
whyz3rman Posted October 5, 2007 Share Posted October 5, 2007 No, no one knows. Perhaps if you gave details about your problem or posted the code you're using we could help.Hi guys, I have a similar question. I've seen some threads around the forum regarding this topic, but nothing helpful to me. I am trying to learn PHP from scratch (I've had some experience in customizing the existing code, but now I am trying to do something myself) and I am going through PHP tutorials on this page and I am stuck at this point:My SQL INSERT ----> Insert Data From a Form Into a DatabaseManual insertion at the beginning works, db is fine, tables are fine.But when I try to insert it from a form (via combination of html and insert.php) ---> the html part works fine, but when I click on submit query, Dreamweaver opens instead of entry being inserted in the sql via insert.php script as shown in the tutorial. I guess it doesn't recognize the php structure? What am I doing wrong?<html><body><form action="insert.php" method="post">Ime: <input type="text" name="ime" />Priimek: <input type="text" name="priimek" />Klub: <input type="text" name="klub" />Starost: <input type="text" name="starost" />Goli <input type="text" name="goli" /><input type="submit" /></form></body></html> don't mind the foreign words (they are name, surname, club, age, goals) <?php$con = mysql_connect("localhost","******","*******");if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("hokej", $con);$sql="INSERT INTO igralec (Ime, Priimek, Klub, Starost, Goli)VALUES('$_POST[ime]','$_POST[priimek]','$_POST[klub]','$_POST[starost]','$_POST[goli]' )";if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); }echo "1 record added";mysql_close($con)?> At first glance it seems to be equal to the one in the tutorial.Thanks Link to comment Share on other sites More sharing options...
Synook Posted October 7, 2007 Share Posted October 7, 2007 Your not trying to run your site from your local machine are you? You need to either upload it to a server with PHP and MySQL or install a localhost with PHP and MySQL on your computer, or else the PHP pages will not run. Link to comment Share on other sites More sharing options...
whyz3rman Posted October 8, 2007 Share Posted October 8, 2007 Your not trying to run your site from your local machine are you? You need to either upload it to a server with PHP and MySQL or install a localhost with PHP and MySQL on your computer, or else the PHP pages will not run.I have a working XAMPP on my computer. PHP is working 100%, mysql is working 100% (both setup via localhost). Like I said, manual insertion (in the tutorial) via script below worked perfectly, but via form it doesn't. It opens dreamweaver just like if the apache (php) wouldn't recognize the code. Any other ideas?<?php$con = mysql_connect("localhost","*****","*******");if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("hokej", $con);mysql_query("INSERT INTO igralec (Ime, Priimek, Klub, Starost, Goli) VALUES ('John', 'Deere', 'Bears', '32', '6')");mysql_query("INSERT INTO igralec (Ime, Priimek, Klub, Starost, Goli) VALUES ('Jim', 'Horton', 'Devils', '26', '4')");mysql_close($con);?> Link to comment Share on other sites More sharing options...
justsomeguy Posted October 8, 2007 Share Posted October 8, 2007 What is the filename of the form processing script? Link to comment Share on other sites More sharing options...
whyz3rman Posted October 9, 2007 Share Posted October 9, 2007 What is the filename of the form processing script?'auto.html' and 'insert.php' Link to comment Share on other sites More sharing options...
justsomeguy Posted October 9, 2007 Share Posted October 9, 2007 If you're trying to open a PHP file and it is showing in Dreamweaver then the web server is not running the PHP code. Either the web server is set up incorrectly, or you aren't using the web server to run it. Make sure that the URL in your browser starts with http, if it doesn't then you aren't using the server. Link to comment Share on other sites More sharing options...
whyz3rman Posted October 10, 2007 Share Posted October 10, 2007 If you're trying to open a PHP file and it is showing in Dreamweaver then the web server is not running the PHP code. Either the web server is set up incorrectly, or you aren't using the web server to run it. Make sure that the URL in your browser starts with http, if it doesn't then you aren't using the server.Thanks for the tip man. It was exactly what you predicted. Php files cannot be just doubleclicked, to be shown in IE, while html can. So that explained why php files worked: "http://localhost/ucim/insert.php"; I just doubleclicked on the html component and didn't even check the title bar. Of course the IE used the absolute path: "D:\web\xampp\htdocs\ucim\auto.html" which caused the problem.Thanks guys. Link to comment Share on other sites More sharing options...
justsomeguy Posted October 10, 2007 Share Posted October 10, 2007 If you want to double-click the form to open it, but still have PHP work, just set the full path in the form action including the http, that way when you click submit even if you're just looking at a local file it will submit to the web server. Link to comment Share on other sites More sharing options...
whyz3rman Posted October 11, 2007 Share Posted October 11, 2007 thanks again. I have another quick question. What is the easisest way to define the attributes of the (SQL to HTML) output table (font, color, width, border color, background color). I assume you have to add tags to the red line below?______________________________________________________<body bgcolor="#CC3333" text="#FFFF66" link="#00FF00"> <?php $con = mysql_connect("localhost","*****","*******"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("hokej", $con); $result = mysql_query("SELECT * FROM igralec"); echo "<table border='1'> <tr> <th>Ime</th> <th>Priimek</th> </tr>";while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['Ime'] . "</td>"; echo "<td>" . $row['Priimek'] . "</td>"; echo "</tr>"; } echo "</table>"; mysql_close($con); ?> Link to comment Share on other sites More sharing options...
justsomeguy Posted October 11, 2007 Share Posted October 11, 2007 Look up table attributes on the w3schools site, or check the CSS reference. It's probably better to use CSS properties to style the table. Link to comment Share on other sites More sharing options...
whyz3rman Posted October 12, 2007 Share Posted October 12, 2007 thumbs up. thanks Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.