PHP无法插数据到mysql,无法使用php将数据插入到mysql中

i know this is a beginner's question .I am working on a bloodbank database project with html,php and mysql. Here as an administrator,i am trying to send messages to users.At first i am trying to see if the user with the username is present in the database.if he is present i am inserting the username and messages into the table called usermessages But i am not able to insert the data.i am getting the message "message sent successfully",but in reality it is not getting updated in the database.So here is my code,i can assure all that no spelling mistake is present in database or in phpcode.

session_start();

$host="localhost"; // Host name

$username="root"; // Mysql username

$password=""; // Mysql password

$db_name="bloodbank"; // Database name

$tbl_name="users"; // Table name

$tblname="usermessages";

// Connect to server and select databse.

mysql_connect("$host", "$username", "$password")or die("cannot connect");

mysql_select_db("$db_name")or die("cannot select DB");

// username and messages is sent from form

$username=$_POST['username'];

$sql="SELECT * FROM $tbl_name WHERE username='$username'";

$result=mysql_query($sql);

// Mysql_num_row is counting table row

$count=mysql_num_rows($result);

if($count==1)

{

$mysql="INSERT INTO tblname(username, messages)

VALUES

('$_POST[username]','$_POST[messages]')";

echo "Message Sent Successfully";

}

else

{

echo "No user with that username found in the database";

}

?>

解决方案

Try to execute the query

$mysql="INSERT INTO $tblname(username, messages)

VALUES ('$_POST[username]','$_POST[messages]')";

$return = mysql_query($my_sql);

echo "Message Sent Successfully";

You just forgotted to execute this insert query

And my advice is dont use mysql_* functions as they are depricated,use either mysqli_* functions or PDO Statements,and while you are playing with the post variables try to escape them like

mysql_real_escape_string($_POST['messages']);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值