将数据插入MySql DB并显示插入是否成功 [英] Insert data to MySql DB and display if insertion is success or failure

查看:60
本文介绍了将数据插入MySql DB并显示插入是否成功的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将数据插入MySQL数据库,以防万一插入失败,我需要给出一条适当的错误消息来指示.根据下面的代码,如果插入成功或失败,我将显示Echo-ing消息Inserted.

I am inserting data to a MySQL DB, In case if the insertion fails i need to give an appropriate error message indicating so. According to my code below i will be Echo-ing the message Inserted if the insertion was success or failure.

我要做的是仅在数据插入成功时回显Inserted消息,如果失败则返回fail.我该如何修改我的代码?

What i want to do is to echo the Inserted message only when the data insertion is a success, and return fail if it fails. How can i modify my code to do so ?

<?php

mysql_connect("localhost", "root", "123") or die(mysql_error());
mysql_select_db("swm_database") or die(mysql_error());

mysql_query("INSERT INTO PEOPLE (NAME ) VALUES ('COLE')")or die(mysql_error()); 

echo "Inserted";
?>

推荐答案

$result = mysql_query("INSERT INTO PEOPLE (NAME ) VALUES ('COLE')"));
if($result)
{
echo "Success";

}
else
{
echo "Error";

}

这篇关于将数据插入MySql DB并显示插入是否成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆