变量不会插入MySQL表中

时间:2014-03-26 00:50:13

标签: php mysql sql database variables

我有这个脚本,它接受一个变量并将其放入表中。我的connect.php文件是正确的,并选择了正确的数据库,但由于某种原因,这些数据不会插入。它说它成功但没有任何东西放在桌子上

我的剧本

if(!empty($_POST['phrase'])){
    $phrase = $_POST['phrase'];
    $phrase = mysql_real_escape_string($phrase);
    $phrase = preg_replace("/[^A-Za-z ]/", "", $phrase);
    $query5 = mysql_query('SELECT phrase FROM dictionary WHERE phrase = "'.$phrase.'" '); // Check the database 


    if(mysql_num_rows($query5)>0){
        null;
    }else{
        echo $phrase;
        $query10 = "INSERT into dictionary(adjective, noun, phrase, subject, verb) values(NULL, NULL, '$phrase', NULL, NULL)";
        $result = mysqli_query($query10); 
        echo "<pre>Debug: $query10</pre>\m";
        //$result = mysqli_query($con, $query);
        if ( false===$result ) {
            printf("error: %s\n");
        }
        else {
            echo 'done.';
        }
        mysqli_close();
    }
    //Debugging Purpose
    echo '<span class="error" > -'. $phrase .'</span>';
}

回声完成了。但表格没有更新

这是我的表格设置方式

-- Database: `dictionary`
--
CREATE DATABASE IF NOT EXISTS `dictionary` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;
USE `dictionary`;

-- --------------------------------------------------------

--
-- Table structure for table `adjective`
--

CREATE TABLE IF NOT EXISTS `adjective` (
`id` int(10) NOT NULL,
`adjective` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `adjective` 
--

INSERT INTO `adjective` (`id`, `adjective`) VALUES
(1, 'orange'),
(2, 'blue');

-- --------------------------------------------------------

--
-- Table structure for table `noun`
--

CREATE TABLE IF NOT EXISTS `noun` (
`id` int(10) NOT NULL,
`noun` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `noun`
--

INSERT INTO `noun` (`id`, `noun`) VALUES
(1, 'tamaleh');

-- --------------------------------------------------------

--
-- Table structure for table `phrase`
--

CREATE TABLE IF NOT EXISTS `phrase` (
`id` int(10) NOT NULL,
`phrase` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `phrase`
--

INSERT INTO `phrase` (`id`, `phrase`) VALUES
(0, 'Hello There'),
(1, 'Hello World');

-- --------------------------------------------------------

--
-- Table structure for table `subject`
--

CREATE TABLE IF NOT EXISTS `subject` (
`id` int(10) NOT NULL,
`subject` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `subject`
--

INSERT INTO `subject` (`id`, `subject`) VALUES
(1, 'I'),
(2, 'The');

-- --------------------------------------------------------

--
-- Table structure for table `verb`
--

CREATE TABLE IF NOT EXISTS `verb` (
 `id` int(10) NOT NULL,
 `verb` text NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `verb`
--

INSERT INTO `verb` (`id`, `verb`) VALUES
(1, 'jumping');

1 个答案:

答案 0 :(得分:-1)

注意:

让我们首先重新建立与MySQL数据库的连接:

$con = new mysqli("host", "username", "password", "dictionary"); /* REPLACE NECESSARY DATA INSIDE */

/* CHECK CONNECTION */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

然后检查表单中的每个POST数据:

if(!empty($_POST["phrase"])){ /* IF SUBMITTED phrase DATA IS NOT EMPTY */

  /* LETS CHECK FIRST IF THE PASSED ON phrase DATA ALREADY EXIST IN THE phrase TABLE */
  if($stmt = $con->prepare("SELECT * FROM phrase WHERE phrase = ?")){
    $stmt->bind_param("s", $_POST["phrase"]); /* BIND THE PASSED ON DATA TO THE QUERY */
    $stmt->execute(); /* EXECUTE THE QUERY */
    $stmt->store_result(); /* STORE THE RESULT */
    $noofphrase = $stmt->num_rows; /* GET THE NUMBER OF RESULTS OF THE EXECUTE QUERY */

      if($noofphrase ==0){ /* IF phrase IS NOT YET IN THE phrase TABLE */
        $stmt2 = $con->prepare("INSERT INTO phrase (phrase) VALUES (?)"); /* START INSERT QUERY */
        $stmt2->bind_param("s", $_POST["phrase"]); /* BIND THE PASSED ON DATA TO THE QUERY */
        $stmt2->execute(); /* EXECUTE THE INSERT QUERY */
        $stmt2->close(); /* CLOSE THE INSERT QUERY PREPARED STATEMENT */
      }

    $stmt->close(); /* CLOSE THE PREPARED STATEMENT */
  } /* END OF PREPARED STATEMENT */

} /* END OF IF phrase IS NOT EMPTY */

然后对adjectivenounsubjectverb重复此操作。

不那么繁琐且人口较少的代码

这是一项繁琐的工作。代码将看起来已填充。那么为什么我们不创建一个函数,它将参数化键入的数据,表名和列名。这是一种更好的方法,因为它们以相同的模式运行。

<?php

  function CheckAndInsert ($data, $tablename, $columnname){

    global $con;

    if(!empty($data)){ /* CHECK IF KEYED-IN DATA IS NOT EMPTY */

      /* LETS CHECK FIRST IF THE PASSED ON phrase DATA ALREADY EXIST IN THE phrase TABLE */
      if($stmt = $con->prepare("SELECT * FROM ? WHERE ? = ?")){
        $stmt->bind_param("sss", $tablename, $columnname, $data); /* BIND THE PASSED ON DATA TO THE QUERY */
        $stmt->execute(); /* EXECUTE THE QUERY */
        $stmt->store_result(); /* STORE THE RESULT */
        $noofresult = $stmt->num_rows; /* GET THE NUMBER OF RESULTS OF THE EXECUTE QUERY */

          if($noofresult ==0){ /* IF phrase IS NOT YET IN THE phrase TABLE */
            $stmt2 = $con->prepare("INSERT INTO ? (?) VALUES (?)"); /* START INSERT QUERY */
            $stmt2->bind_param("sss", $tablename, $columnname, $data); /* BIND THE PASSED ON DATA TO THE QUERY */
            $stmt2->execute(); /* EXECUTE THE INSERT QUERY */
            $stmt2->close(); /* CLOSE THE INSERT QUERY PREPARED STATEMENT */
          } /* END OF CHECKING NO OF RESULTS FROM FIRST QUERY */

        $stmt->close(); /* CLOSE THE PREPARED STATEMENT */
      } /* END OF PREPARED STATEMENT */

    } /* END OF CHECKING

  } /* END OF FUNCTION */

?>

因此,当用户提交表单时,只需调用此函数。

CheckAndInsert($_POST["phrase"], "phrase", "phrase");
CheckAndInsert($_POST["adjective"], "adjective", "adjective");
CheckAndInsert($_POST["noun"], "noun", "noun");
CheckAndInsert($_POST["subject"], "subject", "verb");
CheckAndInsert($_POST["verb"], "subject", "verb");