开发者

php: connecting to a database and and inserting a row of data

I just don't know what I;m doing wrong... I have a php script that is supposed to insert values into my database, but everytime that the code hits this part of the script it hangs...

Im a newbie, I looked at tutorials online, but clearly I don't understand what Im doing wrong..

// Make a MySQL Connection
$link = mysql_connect("jdbc:mysql://mysqlx.server.net/MyDB", "MyUserName", "MyPassword");

//connect to my database
$link.mysql_select_db("TalesDB");

//insert 
$link.mysql_query("INSERT INTO table (ID, name, comment) VALUES(" +
                  $_GET['Key']  + ", '" +
                  $("#leaveComment").find("input").val() + "', '" +
                  $("#leaveComment").find("textarea").val() + "' ) "); 

Please note that I use $("#leaveComment").find("input").val() and $("#leaveComment").find(开发者_Python百科"textarea").val() + "' ) "); earlier in the script - and they are validated as text

Please help! Any idea what Im doing wrong?


Yes you have mixed you JavaScipt and PHP together. The two can definitely work together but not like this :)

Use JQuery or just HTML forms to post data back to a server side script. You can use JQuery's ajax or post methods to accomplish this.

Then in your php script you can do something like this to insert a db record:

<?php

$con = new PDO('mysql:dbname=test;host=127.0.0.1', 'username', 'password');

$stmt = $con->prepare('INSERT INTO `table` (`id`, `name`, `comment`) 
    VALUES (:id, :name, :comment)');

$stmt->bindParam(:id, $_POST['id']);
$stmt->bindParam(:name, $_POST['name']);
$stmt->bindParam(:comment, $_POST['comment']);

$result = $stmt->execute();

?>

I have used PHP PDO to connect and run the query. I'd recommend using it over mysql_query because of the security benefit of prepared statements. For more information take a look at the PDO documentation page.

An important thing to note is that the dollar sign $ is used for different purposes in JQuery and PHP. In JQuery it is a shorthand for the JQuery object and allows you to easily perform element selection. in PHP it is used to identify user declared variables.


Wow, you expect too much from your humble PHP intepreter.

You're mixing .-style object access on a plain connection resource. And jQuery too.

For example instead of

$link.mysql_select_db("TalesDB");

you should do

mysql_select_db("TalesDB", $link);

Please recheck all the documentation from zero, don't confuse PHP with any other object-oriented platform, and certainly not with a (primarily) client-side one!


You're using Javascript when you should be using php.

PHP is a server-side language, which means the php code will be executed before the HTML is sent to you.

Javascript is a client-side language, which means the code will be executed after the page has been sent to you.


i think, your problem is that you're trying to access DOM elements via jQuery on server side. on server side you have $_GET and/or $_POST arrays with form parameters passed from client webbrowser to your server script

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜