插入php无法链接到phpmyadmin,当我插入数据时,它不会显示在phpmyadmin中 [英] insert php cannot link to phpmyadmin , when i insert data , it will not display in phpmyadmin
本文介绍了插入php无法链接到phpmyadmin,当我插入数据时,它不会显示在phpmyadmin中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的问题:我的插入 php 在输入详细信息时无法将数据保存到 phpmyadmin.localhost:locahost ,用户名:root 密码:"" 数据库名:b_database 表名:my_librarytablecolumn:2 包含:isbn(primary key) 和 title
my question : my insert php cannot save data to phpmyadmin when it enter the details. localhost:locahost , username:root password:"" databasename:b_database tablename:my_library tablecolumn:2 contains : isbn(primary key) and title
index.php
<html>
<head>
<meta name="description" content="Php Code for View, Search, Edit and Delete Record" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Search Library Record</title>
</head>
<body>
<center><h1><u>Library Database</u></h1></center>
<form name="search" method="post" action="search.php">
<table style=" border:1px solid silver" cellpadding="10px" cellspacing="0px"
align="center">
<tr>
<td colspan="3" style="background:#0066FF; color:#FFFFFF; fontsize:
20px">Search</td></tr>
<tr>
<td>Enter Search Keyword</td>
<td><input type="text" name="search" size="40" /></td>
<td><input type="submit" value="Search" /></td>
</tr>
<tr>
<td colspan="3"> </td></tr>
<tr bgcolor="#CCCCCC">
<th><a href="add.php">Add Record</a></th>
<th><a href="del.php">Delete Record</a></th>
<th><a href="del.php">Update Record</a></th>
</tr>
</table>
</form>
</body>
</html>
add.php
<?php
$conn_error = 'Could not connect.';
$mysql_host = 'localhost';
$mysql_user = 'root';
$mysql_pass = '';
$mysql_db='b_database';
if(!@mysql_connect($mysql_host, $mysql_user,$mysql_pass)|| !@mysql_select_db($mysql_db)){
die($conn_error);
}
?>
<html>
<head>
<meta name="description" content="Php Code for View, Search, Edit and Delete Record" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Add Student Record</title>
</head>
<body>
<center>
<h1><u>Library Database</u></h1>
</center>
<?
if($_POST["do"]=="store")
{
$isbn=$_POST["isbn"];
$title=$_POST["title"];
if($query_run = mysql_query($query)){
$query="insert into mylibrary value('$isbn','$title')";
mysql_query($query);
echo "Successfully store in DATABASE";
}
?>
<form name="add" method="post" action="add.php">
<table style=" border:1px solid silver" cellpadding="5px" cellspacing="0px"
align="center" border="0">
<tr>
<td colspan="4" style="background:#0066FF; color:#FFFFFF; fontsize:
20px">ADD RECORD</td>
</tr>
<tr>
<tr>
<td>Enter ISBN</td>
<td><input type="text" name="isbn" size="20"></td>
</tr>
<tr>
<td>Enter TITLE</td>
<td><input type="text" name="title" size="20"></td>
</tr>
<tr>
<td colspan="4" align="center"><input type="hidden" name="do" value="store">
<input type="submit" value="ADD RECORD"></td>
</tr>
</table>
</form>
<p align="center"><a href="index.php">Go Back to Home</a></p>
<?
include("search.php");?>
</body>
</html>
推荐答案
存在多个问题.其中一些:
There are multiple problems. Some of them:
- 您的查询字符串有误.你有
VALUE
而不是VALUES
- 您将查询字符串分配给
$query
执行后 - 停止使用已弃用的
mysql_*
扩展并切换到 <代码>mysqli_* 或PDO
- 验证和清理用户输入
- 学习和使用准备好的语句而不是插入查询字符串.后者是 sql 注入的大门.
- Your query string is wrong. You have
VALUE
instead ofVALUES
- You assign a query string to
$query
after you execute it - Stop using deprecated
mysql_*
extension and switch to eithermysqli_*
orPDO
- Validate and sanitize user input
- Learn and use prepared statements instead of interpolating query strings. The latter is a wide open door to sql injections.
现在回到你眼前的问题.尝试改变这部分
Now back to your immediate problem. Try to change this part
if($query_run = mysql_query($query)){
$query="insert into mylibrary value('$isbn','$title')";
mysql_query($query);
echo "Successfully store in DATABASE";
}
像这样
$query = "INSERT INTO mylibrary (isbn, title) VALUES('$isbn', '$title')";
$result = mysql_query($query);
if($result) {
echo "Successfully stored in DATABASE";
} else {
echo "Something went wrong: " . mysql_error();
}
这篇关于插入php无法链接到phpmyadmin,当我插入数据时,它不会显示在phpmyadmin中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文