2016-11-03 2 views
-1

을 통해 MySQL을 삽입하기되지 않는다 (이 3 열이 포함되어 있습니다).데이터가 나는 테이블에 하나 개의 요소를 삽입하려고 PHP

코드 :

<?php 
require 'connect.db.php'; 

$msg = 'msg'; 
mysql_query("INSERT INTO messages('Message') VALUES ('$msg')"); 
$result1 = mysql_query("SELECT * FROM messages ORDER by Msg_ID DESC"); 
while ($extract = mysql_fetch_array(result1)) { 
    echo $extract['Message']; 
} 


?> 

나는이 오류가 무엇입니까 그것을 실행하는 경우 : mysqli를 사용 후

Notice: Use of undefined constant result1 - assumed 'result1' in C:\xampp\htdocs\SocialNetwork\chat\chat.php on line 7

Warning: mysql_fetch_array() expects parameter 1 to be resource, string given in C:\xampp\htdocs\SocialNetwork\chat\chat.php on line 70

데이터가 DB에 삽입지고 있지 않습니다 ..

오류 (대체를 MySQL의) :

Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\xampp\htdocs\SocialNetwork\chat\chat.php on line 6

Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\xampp\htdocs\SocialNetwork\chat\chat.php on line 7

Notice: Use of undefined constant result1 - assumed 'result1' in C:\xampp\htdocs\SocialNetwork\chat\chat.php on line 8

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, string given in C:\xampp\htdocs\SocialNetwork\chat\chat.php on line 8

+0

사용 mysqli : 그래서 지금 당신이 mysqli_ 기능으로 전환,

while ($extract = mysql_fetch_array($result1)) { 

좋아을, 여기 다른 이야기입니다 MySQL이 사용되지 PHP 7 – Kumar

+0

제거된다 *** http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php ([mysql_로 '*'기능을 사용하여 STOP]주세요) *** [이 확장 기능] (http://php.net/manual/en/migration70.removed-exts-sapis.ph p)가 PHP 7에서 제거되었습니다. [PDO]에 대한 [prepared] (http://en.wikipedia.org/wiki/Prepared_statement) 문장에 대해 알아보십시오. (http://php.net/manual/en/pdo.prepared -statements.php)와 [MySQLi (http://php.net/manual/en/mysqli.quickstart.prepared-statements.php)와 [정말 아주 쉽게, PDO를 사용하는 것이 좋습니다 (HTTP : // jayblanchard. net/demystifying_php_pdo.html). –

+0

[Little Bobby] (http://bobby-tables.com/)에서 *** [귀하의 스크립트는 SQL 주입 공격의 위험에 있습니다.] (http://stackoverflow.com/questions/60174/how-can- i-prevent-sql-in-php) ***. 심지어 [이스케이프 문자열] (http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) 안전하지 않습니다! *** SQL 인젝션! *** * 더 이상 아침 식사가 아닙니다! * –

답변

1

잘못된 따옴표. 사용 열 이름을 `을 백 틱 :

INSERT INTO messages(`Message`) VALUES ('$msg') 
-- ------------------^-------^ 

참고 :mysql_*를 사용되지 않습니다. mysqli_* 또는 PDO를 사용하십시오.

또한, 다른 오류를 들어, $ 추가하는 것을 잊었다 :

$conn = mysqli_connect($host, $user, $pass, $db); 

// And change all the functions to: 
mysqli_query($conn, "SELECT ..."); 
mysqli_query($conn, "INSERT ..."); 
+0

같은 오류, .... – IamNOOB

+0

확실히,하지만 설명하지 않습니다'Notice : 정의되지 않은 상수 result1 사용 - 'mysql_fetch_array (result1)'여기에있는'result1''을 사용합니다. <<< –

+0

mysqli 또는 pdo를 사용하여 기다려주십시오. – IamNOOB

관련 문제