私はphpを使用してテーブルにデータを挿入しようとしています。私はユーザーを登録するためにこれを行い、それは正常に動作しますが、私は今レビューを提出するためにそれをやろうとしています。私は答えを探してみましたが、問題の内容を把握できていないようです。phpを使用してMySQLデータベースにデータを挿入する問題
私はいくつかのデバッグを行っており、変数が正しいデータを格納していること、そしてPHPが正しいテーブルに接続していることを知っていますが、変数をテーブルに挿入しようとすると動作しません。
は、ここに私のPHPです:
<?php
session_start();
$dbhost = 'localhost';
$dbuser = 'rlr17';
$dbpass = 'rlr17';
$dbname = 'rlr17';
$dbtable = 'bookclubreviews';
// connect to the database
$db = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql database '. mysql_error());
$bookID=$_GET["bookID"];
$userID=$_GET["userID"];
$reviewTitle=$_GET["reviewTitle"];
$reviewContent=$_GET["reviewContent"];
$rating=$_GET["ratingToSubmit"];
$reviewID= uniqid($id).date("ymd");
if (!$db) {
die('Not connected : ' . mysql_error());
} else {
}
// select the table
$dbselect = mysql_select_db($dbname);
if (!$dbselect) {
die ('Can\'t use $dbname : ' . mysql_error());
} else {
echo "connected to $dbname";
}
if ($bookID=='') {
$bookID="empty";
}
if ($userID=='') {
$userID="empty";
}
if ($reviewTitle=='') {
$reviewTitle="empty";
}
if ($reviewContent=='') {
$reviewTitle="empty";
}
if ($rating=='') {
$rating="empty";
}
//the next 4 lines are to test that the right table is being connected to - it is, this works
$sql1="SELECT * FROM $dbtable WHERE userID='$userID'";
$result1 = mysql_query($sql1,$db);
$result4 = mysql_num_rows($result1);
echo "worked - $result4 <br>";
//This is the bit that I can't get to work.
$insert = "INSERT INTO $dbtable VALUES('$userID','$bookID','$reviewTitle','$reviewContent','$rating')";
$result=mysql_query($insert,$db);
if ($result) {
echo "review submitted". ".<br>";
$data = '';
include('home.php') ;
} else {
echo 'Error with submitting data <br>' . $bookID . $userID . $reviewTitle . $reviewContent . $rating . $reviewID . "<br> db: " .$db;
}
mysql_close($db);
?>
そして、これは screenshot of how my table is set up
であり、これは私の仕事へのリンクです - 任意のヒントをいただければ幸いですhttp://itsuite.it.brighton.ac.uk/rlr17/bookClub/insertReview.php?bookID=5&userID=rlr17&reviewTitle=Test&reviewContent=test&ratingToSubmit=4
!
mysql_関数は2013年以降廃止されていると** PHPには存在しない列の名前を指定します。もう**、使用を中止してください。 [PHPでmysql_ *関数を使うべきではないのですか?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php)を参照してください。さらに、あなたのコードはSQLインジェクション攻撃に対して広く開かれています。 – Oldskool
[Little Bobby](http://bobby-tables.com/)は、[あなたのスクリプトはSQLインジェクション攻撃の危険にさらされていると言います。](http://stackoverflow.com/questions/60174/how-can-i-prevent -sql-injection-in-php)を実行します。 [文字列をエスケープする](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string)でも安全ではありません! –
[mysql_ *関数の使用をやめてください](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php)。 [これらの拡張機能](http://php.net/manual/en/migration70.removed-exts-sapis.php)はPHP 7で削除されました。[prepared](http://en.wikipedia.org/ [PDO](http://php.net/manual/en/pdo.prepared-statements.php)および[MySQLi](http://php.net/manual/en/mysqli.quickstart)のwiki/Prepared_statement)ステートメント.prepared-statements.php)、PDOの使用を検討してください。[これは本当に簡単です](http://jayblanchard.net/demystifying_php_pdo.html)。 –