我創建一個記錄系統,爲此,我希望用戶保存的個人資料圖片帶基本信息(姓名,用戶名,密碼)一起發送圖像。對於這一點,我已經編寫了以下文件:PHP - 無法通過mysqli_stmt_send_long_data
HTML表單(在的index.php):
<form id='signupform' enctype="multipart/form-data">
<table style='margin: 0px auto; border-collapse: separate; border-spacing: 0 1em; color: #ffffff;'>
<tr>
<th colspan='2'>
<h2>SIGN UP</h2>
<p>
Please fill out ALL the fields
</p>
</th>
</tr>
<tr>
<td colspan='2'>
<div id='signuperror' style='display: block; width: 100%; height: 20px; border: 2px solid #ff0000; border-radius: 10px; background-color: #ffffff; color: #ff0000; text-align: center; opacity: 0;'>Username is taken!</div>
</td>
</tr>
<tr>
<td style='text-align: right;'>
First Name:
</td>
<td>
<input id='fname' type='text' name='first' placeholder='First Name' onkeyup="checkComplete('signup');" />
</td>
</tr>
<tr>
<td style='text-align: right;'>
Last Name:
</td>
<td>
<input id='lname' type='text' name='last' placeholder='Last Name' onkeyup="checkComplete('signup');" />
</td>
</tr>
<tr>
<td colspan='2' style='text-align: center;'>
<input id='usrimg' type='file' accept='image/*' name='userimage' style='display: none;' />
<table style='width: 100%;'>
<tr>
<td style='width: 90%;'>
<button type='button' class='popbtn' onclick='browseImage();' style='width: 100%;'>Profile Image</button>
</td>
<td style='width: 10%;'>
<img id='imgstats' style='vertical-align: middle; display: none;' src='imgsuccess.png' />
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td style='text-align: right;'>
Username:
</td>
<td>
<input id='username' type='text' name='uid' placeholder='Username' onfocusout="checkUID(this.value);" onkeyup="checkComplete('signup');" />
</td>
</tr>
<tr>
<td style='text-align: right;'>
Password:
</td>
<td>
<input id='password' type='password' name='pwd' placeholder='Password' onkeyup="checkComplete('signup');" />
</td>
</tr>
<tr>
<td colspan='2' style='text-align: center;'>
<button type='button' id='signupbtn' class='popbtn' disabled='disabled'>SIGN UP</button>
</td>
</tr>
</table>
</form>
因此,CSS可能混淆了一點,但基本形式如下:
<form id='signupform' enctype="multipart/form-data">
<input id='fname' type='text' name='first' />
<input id='lname' type='text' name='last' />
<input id='usrimg' type='file' name='userimage' />
<input id='username' type='text' name='uid' />
<input id='password' type='password' name='pwd' />
<button type='button' id='signupbtn'>SIGN UP</button>
</form>
所以signupbtn
按鈕具有附着在文檔加載一個onclick
事件(也是在index.php文件,我沒有展示這一切,因爲它是不適合此):
$(function() {
$('#signupbtn').click(function(e) {
e.preventDefault();
e.stopImmediatePropagation();
$.ajax({
type: "POST",
url: "signup.php",
data: $('#signupform').serialize(),
success: function(response){
//...
}
});
return false;
});
//...
});
所以我用AJAX調用文件signup.php
提交表單不清爽。然後在該文件:
<?php
session_start();
include('connect.php');
$first = $_POST['first'];
$last = $_POST['last'];
$img = mysqli_real_escape_string(file_get_contents($_FILES['userimage']['tmp_name']));
$uid = $_POST['uid'];
$pwd = $_POST['pwd'];
$query = "insert into Users (first, last, img, uid, pwd) values (?,?,?,?,?)";
$stmt = mysqli_prepare($connect, $query);
mysqli_stmt_bind_param($stmt, 'ssbss', $first, $last, $img, $uid, $pwd);
mysqli_stmt_send_long_data($stmt, 2, $img);
$res = mysqli_stmt_execute($stmt);
echo $res;
?>
我檢索所有表單中的信息,然後使用mysqli_stmt_send_long_data
在插入我的形象成表,這是在connect.php創建嘗試:
<?php
$host_name = "xxx";
$database = "xxx";
$user_name = "xxx";
$password = "xxx";
$connect = mysqli_connect($host_name, $user_name, $password, $database);
if(mysqli_connect_errno()) {
echo '<p>Failed to connect to MySQL: '.mysqli_connect_error().'</p>';
}
else {
//echo '<p>Connection to MySQL server successfully established.</p>';
$db_selected = mysqli_select_db($connect, $database);
if ($db_selected) {
$userTableName = 'Users';
$userTableExists = mysqli_query($connect, "select * from $userTableName");
if (!$userTableExists) {
echo 'The table ' . $userTableName . ' does not exist. Creating...<br /><br />';
$sqlCreateTable = "create table " . $userTableName . "(id int(11) not null PRIMARY KEY AUTO_INCREMENT,
first varchar(128) not null,
last varchar(128) not null,
img longblob not null,
uid varchar(128) not null,
pwd varchar(1000) not null)";
$result = mysqli_query($connect, $sqlCreateTable);
if ($result) {
echo 'Table ' . $userTableName . ' created...<br>';
}
else {
die("Cannot create table " . $userTableName . ": " . mysql_error());
}
}
}
else {
die("Cannot use " . $database . ": " . mysql_error());
}
}
?>
由於你會期望,因爲我問這個問題,Ajax成功結束,並插入行。但是,當我檢查phpMyAdmin的在我的網站的數據庫中,我得到了img
領域具有[BLOB - 0 B]
值這隻能意味着圖片上傳不工作。可能是什麼情況?我在send_long_data上找不到任何工作,但也許有人遇到了同樣的問題。提前感謝您提供任何答案。
**永不**存儲純文本密碼。你應該使用['password_hash()'](http://us3.php.net/manual/en/function.password-hash.php)和['password_verify()'](http://us3.php。 net/manual/en/function.password-verify.php)。如果您使用的是5.5之前的PHP版本,請不要**使用MD5或SHA1來散列密碼。相反,您可以使用[此兼容包](https://github.com/ircmaxell/password_compat)。 –
列的字段類型是什麼? –
@Alex Howansky是的,我明白了。這不會是我的最終版本。如果可以的話,我想學習如何加密密碼等,但是我開始進行圖像插入並卡在這裏! – gfcf14