MySQL error code 0 in PHP? Cannot insert data into database with PHP -


so trying insert data php page sql database. page accessible via myself i'm not worried being accessed or sql injectable etc. issue no matter code use doesn't go database. i've tried coding myself, using template codes, taking php.net etc nothing has worked!

it redirects me success message still nothing in database.

code put below , i'll edit of details privacy reasons.

<?php require connect.php     // if values posted, insert them database.     if (isset($_post['username']) && isset($_post['password'])){         $username = $_post['username'];         $isadminb = $_post['isadmin'];         $password = $_post['password'];          $query = "insert `users` (user_name, password, isadmin) values ('$username', '$password', '$isadminb')";         $result = mysql_query($query);         if($result){             $msg = "user created successfully.";         }     }     $link = mysql_connect("localhost", "root", "password"); echo mysql_errno($link) . ": " . mysql_error($link). "\n"; 

the echo mysql_errno($link) . ": " . mysql_error($link). "\n"; code gave me error code 0?

as requested code form previous page.

<form action="account_create_submit.php" method="post"> username: <input type="text" name="username" id="username"> <br /><br /> password: <input type="password" name="password" id="password"> <br /><br /> <span id="isadmin">is admin: yes<input type="radio" name="isadmin" id="1" value="1"> | no<input type="radio" name="isadmin" id="0" value="0"><br /></span> <span id="submit"><input type="submit" value="create account"></span> </form> 

ok changed form code method post. great! data being read correctly although wasn't issue typing in hard data code submit wasn't working @ least future issue resolved already. new error code no longer 0 rather following:

1064: have error in sql syntax; check manual corresponds mysql server version right syntax use near ''user_name', 'password', 'isadmin') values ('testz', 'lol', '1')' @ line 1

connect.php

    <?php $connection = mysql_connect('localhost', 'root', 'password'); if (!$connection){     die("database connection failed" . mysql_error()); } $select_db = mysql_select_db('default_db'); if (!$select_db){     die("database selection failed" . mysql_error()); } 

firstly, of getting misconception password column name:

sure, it's mysql "keyword", not "reserved" word; more specifically, function (see ref). notice there no (r) next "function (keyword) name": https://dev.mysql.com/doc/refman/5.5/en/keywords.html therefore it's valid column name.

ref: https://dev.mysql.com/doc/refman/5.1/en/encryption-functions.html#function_password

ticks required if used in order prevent being recognized "function", not in op's case. so, information , facts straight.

more specifically, if table named password , without spaces between table name , column declaration:

i.e.: insert password(col_a, col_b, col_c) values ('var_a', 'var_b', 'var_c')

which throw syntax error, since table name considered being function.

therefore, proper syntax need read as

insert `password` (col_a, col_b, col_c) values ('var_a', 'var_b', 'var_c') 

(edit:) answer present question; you're using $connection in connection, querying $link along missing db variables passed query , quotes/semi-colon i've outlined here.

that's if want code of yours going, highly discourage it. you're using deprecated mysql library , md5 stated. old technology no longer safe used, nor supported in future php releases.

you're missing semi-colon here require connect.php , quotes.

that should read require "connect.php";

you should remove this:

$link = mysql_connect("localhost", "root", "password"); echo mysql_errno($link) . ": " . mysql_error($link). "\n"; 

you're trying include connection file.

use in connection file: (modified, using connection variable connection parameter)

$connection = mysql_connect('localhost', 'root', 'password'); if (!$connection){     die("database connection failed" . mysql_error()); } $select_db = mysql_select_db('default_db', $connection); if (!$select_db){     die("database selection failed" . mysql_error()); } 

and pass $connection query 2nd parameter.

$result = mysql_query($query, $connection); 

add error reporting top of file(s) right after opening php tag example <?php error_reporting(e_all); ini_set('display_errors', 1); rest of code, see if yields anything.

also add or die(mysql_error()) mysql_query().

if still gives hard time, need escape data.

i.e.:

$username = mysql_real_escape_string($_post['username'], $connection); 

and same others.

use safer method: (originally posted answer)

may total rewrite , using mysqli_ prepared statements.

fill in credentials own.

sidenote: may have replace last s i $isadminb that's if column int.

$link = new mysqli('localhost', 'root', 'password', 'demo'); if ($link->connect_errno) {     throw new exception($link->connect_error, $link->connect_errno); }  if (!empty($_post['username']) && !empty($_post['password'])){     $username = $_post['username'];     $isadminb = $_post['isadmin'];     $password = $_post['password'];  // prepare insert statement     if (!$stmt = $link->prepare('insert `users`            (`user_name`, `password`, `isadmin`)             values (?, ?, ?)')) {         throw new exception($link->error, $link->errno);     }      // bind parameters     $stmt->bind_param('sss', $username, $password, $isadminb);          if (!$stmt->execute()) {             throw new exception($stmt->error, $stmt->errno);         }      }      else{         echo "nothing set, or empty.";     } 

i noticed may storing passwords in plain text. if case, highly discouraged.

i recommend use crypt_blowfish or php 5.5's password_hash() function. php < 5.5 use password_hash() compatibility pack.

you can use pdo example pulled 1 of ircmaxell's answers:

just use library. seriously. exist reason.

don't yourself. if you're creating own salt, you're doing wrong. should using library handles you.

$dbh = new pdo(...);  $username = $_post["username"]; $email = $_post["email"]; $password = $_post["password"]; $hash = password_hash($password, password_default);  $stmt = $dbh->prepare("insert users set username=?, email=?, password=?"); $stmt->execute([$username, $email, $hash]); 

and on login:

$sql = "select * users username = ?"; $stmt = $dbh->prepare($sql); $result = $stmt->execute([$_post['username']]); $users = $result->fetchall(); if (isset($users[0]) {     if (password_verify($_post['password'], $users[0]->password) {         // valid login     } else {         // invalid password     } } else {     // invalid username } 

Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -