无法使用php插入mysql数据库

时间:2016-05-06 09:41:18

标签: php mysql insert

我们正在使用PHP和MySQL为Uni做一个网站项目,虽然一切都在我朋友的计算机上完美运行,但我不会通过PHP向数据库添加数据。代码是相同的,DB也是相同的(导出的),唯一的区别是他在Mac上使用MAMP而我在Windows 7上使用WAMP。我可以通过phpMyAdmin添加数据并通过代码编辑和删除。我刚刚完全重新安装了WAMP,localhost拥有所有权利。

以下是其中一个不起作用的代码:

<?php
$db = mysqli_connect('localhost', 'root', 'root', 'project');
session_start();
date_default_timezone_set('Europe/Paris');
$auteur = $_SESSION['user'];
$texte = nl2br(addslashes(htmlspecialchars($_POST['texte'])));
$date = date('l jS \of F Y');
$type = $_POST['type'];

if ($type == 'article'){
  $titre = addslashes(htmlspecialchars($_POST['titre']));
  $categorie = $_POST['genre'];
  $description = mb_strimwidth($texte, 0, 500,'');
  if (!empty($texte)){
    if (!empty($titre)){
      $query = "INSERT INTO articles VALUES ('', '$auteur', '$texte', '$date', '$description', '$titre', '$categorie')";
      $result = mysqli_query($db, $query) or die('Erreur SQL : '.mysqli_error());
    else {
     echo "<script>alert('Can't publish an article without a title!');  </script>";
    }
  } else {
 echo "<script>alert('Can't publish an article without text!');</script>";
}
?>

虽然这完全正常:

<?php
$db = mysqli_connect('localhost', 'root', 'root', 'project');
session_start();
if (!$db){
    die("Our servers seem to be down. Sorry!");
}

$pseudo = $_SESSION['user'];
$newusername = $_POST['pseudo'];
$newmail = $_POST['mail'];
$newpass = $_POST['password'];
$confpass = $_POST['confpassword'];
$query = "SELECT id FROM utilisateurs WHERE pseudo = '$pseudo'";
$result = mysqli_query($db, $query);
$row = mysqli_fetch_array($result);
$id = $row['id'];


if (!empty($newusername)){
    $query = "UPDATE utilisateurs SET pseudo='$newusername' WHERE id='$id'";
    $result = mysqli_query($db, $query);
}
if (!empty($newmail)){
    $query = "UPDATE utilisateurs SET mail='$newmail' WHERE id='$id'";
    $result = mysqli_query($db, $query);
}
if (!empty($newpass)){
    if(!empty($confpass)){
        if ($newpass == $confpass){
            $query = "UPDATE utilisateurs SET mdp='$newpass' WHERE id='$id'";
            $result = mysqli_query($db, $query);
        } else {
            echo "<script>alert('The confirmation is different from the password');</script>";
        }
    } else {
        echo "<script>alert('Please confirm the password');</script>";
    }
}
echo "<script>alert('The changes have been made.');</script>";
?>

我的配置: Windows configuration

他的配置: Mac configuration

0 个答案:

没有答案