如何允许使用php代码从网站访问sql数据库

时间:2016-02-27 04:57:00

标签: php mysql sql-server database

点击“添加记录到数据库”后,我在网站上收到以下错误消息: 尝试添加记录时出现数据库错误:INSERT命令被拒绝给用户' leaBoss' @' localhost'对于表'产品'

我正在假设,这是我可能完全不正确的地方,我需要调整我的数据库以接受INSERT命令。我认为这是在我的数据库显示时完成的:

image is of privleges in my database

我正在使用第一个,leaBoss在localhost,它显示了所有privedges'

我的连接代码文件如下:

<?php   
// Connection for admin user
if(!defined('ALLOW_ACCESS'))
    die('Direct access to this file is not allowed');
// Information required to connect to MySQL database
define ('DB_HOST', 'localhost');
define ('DB_USER', 'leaBoss');      
define ('DB_PASSWORD', 'assessment');  
define ('DB_NAME', 'dbleaparker');
// connect to the database
$db = @new mysqli (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
// Check whether the connection worked...
if (mysqli_connect_errno()) {
    print '<br />Can\'t connect to database. Please try again later.';
    exit;
}
?>

以及为网页出错的addProduct文件:

<?php
  define('ALLOW_ACCESS', 1); //define a constant to give permission to use include files
    $title = 'Add a product';
    require('../../incAdmin/incHead.php');
    require_once('../../incAdmin/adminConnect.php');
?>
<h2>Add a new product to the Leas Japan Art</h2>  
<?php
if ($_SESSION['loggedIn']) {

if (isset($_POST['cmdSubmit'])) {
  // CREATE VARIABLES from form's POST data
  $categoryID = $_POST['cboCategoryID'];
  $productID = $_POST['txtProductID'];
  $pName = $_POST['txtName'];
  $pPrice = $_POST['txtPrice'];
  $pImage = $_POST['txtImage'];


  // VALIDATE THE FORM (this is very basic - you could make the validation more comprehensive)
  $message = '';

  if (empty($productID)) {
    $message = "ERROR: Please enter a product ID number";
  }
  if (empty($pName)) {
    $message = $message . "\nERROR: Please enter the product name";
  }

  // If no errors, write the record to database
  if ($message == '') {

  $sql = "INSERT INTO category . Products (categoryID, productID, pName, pPrice, pImage) VALUES ('$categoryID','$productID','$pName','$pPrice','$pImage')";
    if ($stmt = $db->prepare($sql)) {
        $stmt->execute();
        $stmt->close();
        $message = 'Record has successfully been added to database';
    }
    else {
        // an error has occurred, so the statement wasn't executed
        print 'Database error while attempting to add record: ' . $db->error;   
        }
  }
}
else {  // this is the first time form will be displayed. Initialise variables.
    $categoryID = '';
    $productID = '';
    $pName = '';
    $pPrice = '';
    $pImage = 'placeholder.jpg';
    $message = '';
}
?>
<form id="frmAddProduct" method="post" action="addProduct.php">
  <p><br />
    <label>Category:</label>
    <select name="cboCategoryID">
            <?php
            //Set up a drop-down list of categories
            $stmt = $db->prepare('SELECT * FROM Category ORDER BY cName');
            $stmt->execute();
            $stmt->bind_result($OUTPUTcategoryID, $OUTPUTcName);
            // while setting up the drop-down list, retain any PREVIOUSLY SELECTED option
            while ($stmt->fetch() ) {
                print '<option ';
                if ($OUTPUTcategoryID == $categoryID) { print 'selected '; }
                print 'value="';
                print $OUTPUTcategoryID;
                print '">';
                print $OUTPUTcName;
                print '</option>';
            }
            $stmt->close();
            ?>
    </select>
    <br /><br />

    <label>Product ID :</label>
    <input type="text" name="txtProductID" id="txtProductID" size="8" value="<?php print $productID; ?>" />
    <br /><br />
    <label>Product Name:</label> 
    <input type="text" name="txtName" id="txtName" size="70" value="<?php print $pName; ?>" />
    <br /><br />

    <label>Product price: &#160;&#160;&#160; $</label> 
    <input type="text" name="txtPrice" id="txtPrice" size="8" value="<?php print $pPrice; ?>" />
     <br /><br />

    <label>Image filename:</label> 
    <input type="text" name="txtImage" id="txtImage" size="30" value="<?php print $pImage; ?>" />
     <em>(must include file extension, eg seascape.jpg)</em><br /><br />

    <input type="submit" name="cmdSubmit" id="cmdSubmit" value="Add record to database" />
    <br /><br />

    <label>Report:</label>
    <textarea name="txtMessage" id="txtMessage" cols="60" rows="4" readonly="readonly"
        style="background-color:#FFF;color:#000; overflow:hidden;"><?php print $message;?></textarea>
  </p>
</form>
<!----------------------------------------------------------------------------->
<?php

}
else {
        print 'ERROR: you are not authorised to access this page';
} 
require('../../incAdmin/incFoot.php');
?>

提前感谢能够提出建议的任何人。

0 个答案:

没有答案