PDA

View Full Version : không lấy được nội dung......



thuanbk
29-12-2008, 13:38
Các bro cho mình hỏi một chút mình mới học php/mysql
mình làm như trong ebook tạo kết nối với database nhưng không được.
mình làm như sau

Đầu tiên tạo file dbconnect.php

<?
$link = mysql_connect("localhost","root","")
or die ("Khong the ket noi toi server");
mysql_select_db("guestbook",$link)
or die ("Khong the ket noi toi csdl")
?>

rồi tạo file sign.php

<HTML>
<TITLE></TITLE>
<HEAD></HEAD>
<BODY>
<H2>Sign my Guest Book!!!</H2>
<FORM method=post action="create_entry.php">
<b>Name:</b>
<input type="text" size="40" name=name class="textbox">
<br>
<br>
<b>Location:</b>
<input type="text" size="40" name=location class="textbox">
<br>
<br>
<b>Email:</b>
<input type="text" size="40" name=email class="textbox">
<br>
<br>
<b>Home page URL:</b>
<input type="text" size="40" name=url class="textbox">
<br>
<br>
<br>
<b>Comments:</b>
<textarea font-face="arial" name=comments cols="40" rows="4" wrap="virtual">
</textarea>
<br>
<br>
<br>
<input type="submit" name="submit" value="OK" class="bottom">
<input type="reset" name="reset" value="Cancel" class="bottom">
</FORM>
</BODY>
</HTML>

tiếp tạo file create_entry.php

<?
include("dbconnect.php");
if($submit == "OK")
{
$query="INSERT INTO guestbook(name,location,email,url,comments) values("$name","$location","$email","$url","$comments")";
mysql_query($query) or die (mysql_error());
?>
<br>
<H2>Cam on!!!</H2>
<H2><a href="view.php">
Xem thong tin vua nhap vao</a>
</H2>
<?
}
Else
{
include("sign.php");
}
?>

ròi tạo file view.php

<html>
<body>
<?php
require("dbconnect.php");
$result = mysql_query("SELECT * FROM guestbook") or die ("khong the ket noi toi database");
while($row = mysql_fetch_array($result))
{
echo"<b>Name: </b>";
echo $row['name'];
echo"<br>\n";
echo"<b>Location: </b>";
echo $row['location'];
echo"<br>\n";
echo"<b>Email: </b>";
echo $row['email'];
echo"<br>\n";
echo"<b>URL:</b>";
echo $row['url'];
echo"<br>\n";
echo"<b>Comments:</b>";
echo $row['comments'];
echo"<br>\n";
echo"<br>\n";
echo"<br>\n";
}
mysql_free_result($result);
?>
<H2><a href="sign.php">Sign My Guest Book!!!</a></H2>
</body>
</HTML>

Trong csdl mình đã tạo database guestbook và bảng guestbook với các trường tuơng ứng.
nhưng kết quả là không nhập được các thông tin từ thẻ form vào csdl vậy là tại sao. Chắc các bro cũng đã làm cái này rồi. Giúp mình với nhá. thanks

wantlove
29-12-2008, 15:13
$name=$_POST['name'];
...
...
$query="INSERT INTO guestbook(name,location,email,url,comments) values("$name","$location","$email","$url","$comments")";
mysql_query($query) or die (mysql_error());

natostr
30-12-2008, 01:21
xem lại cách nối chuỗi trong php nhé ông bạn. Với lại chú ý giùm cách sử dụng đấu " và dấu '. Viết vậy thì chạy bằng mắt thôi.

kylobytes
31-12-2008, 05:15
Sửa:

$query="INSERT INTO guestbook(name,location,email,url,comments) values("$name","$location","$email","$url","$comme nts")";
Thành:

$query="INSERT INTO guestbook(name,location,email,url,comments) values($name,$location,$email,$url,$comments)";

BossFTP
31-12-2008, 08:00
$query="INSERT INTO guestbook(name, location, email, url, comments) VALUES('$name', '$location', '$email', '$url', '$comments')";


Phải có dấu ' nữa...

kylobytes
31-12-2008, 14:43
$query="INSERT INTO guestbook(name, location, email, url, comments) VALUES('$name', '$location', '$email', '$url', '$comments')";


Phải có dấu ' nữa...

Tôi không dùng dấu
` vẫn được mà.