我有管理员档案,我可以编辑我的网站。我尝试过解决方案:$DBConnect = new mysqli("localhost","root","","Ladle");
但它没有用。我也尝试添加unset($ connection);.这也没有用。
当我尝试添加新产品时,我收到错误'无法获取mysqli'。
这是我的代码:
<?php
session_start();
//include_once "admin_check.php";
$dbh = mysqli_connect("host", "user", "password", "db");
if (!$dbh) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
// this will tell you why the connection failed
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($dbh) . PHP_EOL;
mysqli_close($dbh);
?>
<?php
error_reporting(E_ALL);
ini_set('display_errors','1');
?>
<?php
if(isset($_POST['product_name'])){
$product_id = mysqli_real_escape_string($dbh,$_POST['id']);
$product_name = mysqli_real_escape_string($dbh,$_POST['product_name']);
$price = mysqli_real_escape_string($dbh,$_POST['price']);
$details = mysqli_real_escape_string($dbh,$_POST['details']);
$sql=mysqli_query($dbh,"SELECT id FROM products WHERE product_name='$product_name' LIMIT 1");
$productMatch = mysqli_num_rows($sql);
//if($productMatch>0){
//echo 'Sorry you tried to place a duplicate Product Name into the system,<a href="product_list.php">click here</a>';
//exit();
// }
$sql = mysqli_query($dbh,"INSERT INTO products(product_name, price, details,date_added)
VALUES('$product_name','$price','$details',now())")or die(mysqli_error($dbh));
$pid = mysqli_insert_id($dbh);
$newname = 'store'."$pid.jpg";
$url = "images/store";
$urlu = $url;
$basename="/var/tmp/$pid";
if ($_FILES["fileField"]["error"] > 0)
{
echo "Error: " . "$url" . $_FILES["fileField"]["error"] . "<br>";
}
else
{
echo "Upload: " . "$url" . $_FILES["fileField"]["tmp_name"] . "<br>";
echo "Upload: " . "$url" . $_FILES["fileField"]["name"] . "<br>";
echo "Type: " . "$url" . $_FILES["fileField"]["type"] . "<br>";
}
//move_uploaded_file("$urlu", $_FILES["fileField"]["tmp_name"]);
// move_uploaded_file("$url", $_FILES["fileField"]["name"]);
// echo "Stored in: " . "$urlu" . $_FILES["fileField"]["tmp_name"];
move_uploaded_file($_FILES["fileField"]["tmp_name"],
"$url" . "/" . $_FILES["fileField"]["name"]);
echo "Stored in: " . "$url" . "/" .$_FILES["fileField"]["name"];
if (move_uploaded_file("$url/" , $_FILES["fileField"]["name"])) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Upload Failed";
print_r($_FILES);
print_r($_FILES["fileField"]);
exit();
}
}
?>
<?php
$dbh = mysqli_connect("host","user","password", "db") or die ("could not connect to mysql");
$product_list = "";
$sql = mysqli_query($dbh,"SELECT * FROM products");
$productCount = mysqli_num_rows($sql);
if($productCount>0){
while($row = mysqli_fetch_array($sql)){
$id=$row['id'];
$product_name = $row['product_name'];
$details = $row['details'];
$price = $row['price'];
$date_added = strftime('%b %d,%Y',strtotime($row['date_added']));
$product_list.= "'$date_added'-'$id'-'$product_name' <a href=\"product_edit.php\">edit</a>.<a href=\"\">delete</a></br>";
}
mysqli_close($dbh);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Dodawawanie produktu w sklepie</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="ckeditor/ckeditor.js"></script>
<script src="ckeditor/samples/sample.js"></script>
<link rel="stylesheet" media="all"type="text/css" href="ckeditor/contents.css">
<script type="text/javascript">
function validate_form ( ) {
valid =true;
if ( document.form.product_name.value == "" ) {
alert ( "Prosze dodac nazwe produktu." );
valid = false;
} else if ( document.form.price.value == "" ) {
alert ( "Prosze dodac cene." );
valid = false;
} else if ( document.form.details.value == "" ) {
alert ( "Prosze dodac opis i forme w czesci Opis Produktu." );
valid = false;
} else if ( document.form.fileField.value == "" ) {
alert ( "Prosze dodac duze zdjecie Produktu." );
valid = false;
}
return valid;
}
</script>
</head>
<body>
<div><ul><li><?php echo $product_list ?></li></ul></div>
<a name="inventoryForm" id="inventoryForm"></a>
<h3>
↓ Dodaj nowy produkt do sklepu ↓
</h3>
<form method="post" action="product_list.php" enctype="multipart/form-data" name="myForm" id="myform" method="post">
<table width="90%" border="0" cellspacing="0" cellpadding="6">
<tr>
<td width="20%" align="right">Nazwa Produktu</td>
<td width="80%"><label>
<input name="product_name" type="text" id="product_name" size="64" value="<?php echo $product_name; ?>" />
</label></td>
</tr>
<tr>
<td align="right">Cena Produktu</td>
<td><label>
£
<input name="price" type="text" id="price" size="12" value="<?php echo $price; ?>" />
</label></td>
</tr>
<tr>
<td align="right">Opis Produktu(linki, opis i forma)</td>
<td>
<textarea name="details" class="ckeditor" cols="64" rows="5"><?php echo $details; ?><script type="text/javascript">CKEDITOR.replace( 'ckeditor',
{
filebrowserUploadUrl : 'ckeditor/ckupload.php',
filebrowserImageUploadUrl : 'ckeditor/ckupload.php'
});</script></textarea>
</td>
</tr>
<tr>
<td align="right">Obrazek produktu</td>
<td><label>
<input type="file" name="fileField" id="fileField" />
</label></td>
</tr>
<tr>
<td> </td>
<td><label>
<input name="id" type="hidden" value="<?php echo $id ?>" />
<input type="submit" name="button" id="button" value="Teraz dodaj produkt" />
</label></td>
</tr>
</table>
</form>
</body>
</html>
如果有任何建议,我将不胜感激。非常感谢您的帮助。