Jump to content

PHP to MySQL


k3n3t1k

Recommended Posts

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

  • 1 month later...
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

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

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

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...