2013-04-13 131 views
1

我使用php and mysql創建一個註冊頁面,但whene我提交的信息得到它的所有數據插入,但它給我,說一個警告:PHP MYSQL +警告

錯誤:

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\Unnamed Site 2\resources\register.php on line 164 

寄存器。 PHP

<?php require_once('for members/scripts/connect.php'); ?> 


<?php 
ob_start(); 
function countryQuery(){ 

    $countryData = mysql_query("SELECT * FROM country") or die("could select database"); 

    while($record = mysql_fetch_array($countryData)){ 

    echo'<option value="' . $record['country_name'] . '">' . $record['country_name'] . '</option>'; 

    } 

} 


function specializationQuery(){ 

$specData = mysql_query("SELECT * FROM specialization"); 

    while($recordJob = mysql_fetch_array($specData)){ 

    echo'<option value="' . $recordJob['specialization_name'] . '">' . $recordJob['specialization_name'] . '</option>'; 

    } 


} 

function districtQuery(){ 

$distData = mysql_query("SELECT * FROM districts"); 

    while($recorddist = mysql_fetch_array($distData)){ 

    echo'<option value="' . $recorddist['district_name'] . '">' . $recorddist['district_name'] . '</option>'; 

    } 


} 

function villageQuery(){ 

$villageData = mysql_query("SELECT * FROM village"); 

    while($recordvillage = mysql_fetch_array($villageData)){ 

    echo'<option value="' . $recordvillage['village_name'] . '">' . $recordvillage['village_name'] . '</option>'; 

    } 


} 



//default value 
$message = "Fields Marcked with an [*] are Required"; 
    $username = ""; 
    $fname = ""; 
    $lname = ""; 
    $specialization = ""; 
    $email = ""; 
    $pass1 = ""; 
    $pass2 = ""; 
    $district = ""; 
    $village = ""; 

if(isset($_POST['username'])){ 
    $username = $_POST['username']; 
    $fname = $_POST['fname']; 
    $lname = $_POST['lname']; 
    $email = $_POST['email']; 
    $pass1 = $_POST['pass1']; 
    $pass2 = $_POST['pass2']; 
    $bdate = $_POST['birthdate']; 
    $country = $_POST['country']; 
    //$local_adress = $_POST['adress']; 
    $specialization = $_POST['specialization']; 
    $district = $_POST['district']; 
    $village = $_POST['village']; 

    //error handeling 
    if((!$username)||(!$fname)||(!$lname)||(!$email)||(!$pass1)||(!$pass2)||(!$specialization)||(!$district)||(!$village)){ 
    $message = "**** Please insert the Required Fields below ****<br />"; 

    if($fname == "") 
{ 
    $message = $message . "Enter First name<br/>"; 
} 
    if($lname == "") 
{ 
    $message = $message . "Enter Last name<br/>"; 
} 

if($specialization == 0) 
{ 
    $message = $message . "Select Your Job<br />"; 
} 
if($district == 0) 
{ 
    $message = $message . "Select Your District<br />"; 
} 
if($village == 0) 
{ 
    $message = $message . "Select Your Village<br />"; 
} 
if($email == "") 
{ 
    $message = $message . "Enter Email Adress<br/>"; 
} 
    if ($username == "") { 
    $message = $message . "Enter User Name<br/>"; 
    } 

    if($pass1 == "") 
{ 
    $message = $message . "Enter password<br/>"; 
} 

    if($pass2 == "") 
{ 
    $message = $message . "rechek the password <br/>"; 
} 

} 

    elseif(strlen($pass1) <= 8) 
    { 
    $message = $message . "Your Password must be at least 9 charachters<br />"; 
} 
    else if($pass1!=$pass2){ 
    $message = "your password do not match!"; 
    }else{ 
    //securing the data 
    $username = preg_replace("#[^0-9a-z]#i","",$username); 
    $fname = preg_replace("#[^0-9a-z]#i","",$fname); 
    $lname = preg_replace("#[^0-9a-z]#i","",$lname); 
    //$pass1 = sha1($pass1); 

    $email = mysql_real_escape_string($email); 
    // checking for duplicate 
    $user_query = mysql_query("SELECT user_name FROM user WHERE user_name = '$username'LIMIT 1") or die("could not check the username"); 
    $count_username = mysql_num_rows($user_query); 

    $email_query = mysql_query("SELECT email_address FROM user WHERE email_address = '$email'LIMIT 1") or die("could not check the email"); 
    $count_email = mysql_num_rows($email_query); 

    if($count_username > 0){ 
    $message = " your username is alredy in use"; 
    }elseif($count_email > 0){ 
     $message = "your email is alredy in use"; 
    } 
    else{ 

     $query = mysql_query("INSERT INTO user(user_name, first_name, last_name, address, district_name, village_name, birth_date, email_address, specialization_id, password, registered_date)VALUES('$username', '$fname', '$lname', '$country', '$district', '$village', '$bdate', '$email', '$specialization', '$pass1', now())")or die("could not insert data");  

    $message = "you have now been registered"; 
    //from the social website 

這條線164 $的getId

$getid = mysql_fetch_array($query); 
     $_SESSION['user_id'] = $getid['user_rid']; 
     $_SESSION['login'] = 'true'; 
     $_SESSION['login_user'] = $getid['username']; 
//header("Location: home.php"); 
    } 
    } 
} 




?> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Register Page</title> 
<link href="style/stylesheet.css" rel="stylesheet" type="text/css" /> 
<link href="style/imagesGallery.css"rel="stylesheet" type="text/css"/> 
<script src="js/main.js" type="text/javascript"></script> 
<script src="js/jquery.js" type="text/javascript"></script> 
</head> 

<body> 


<table width="200" border="0" align="center"> 
<tr> 
    <tr> 
    <td><img src="web_header copy.jpg" alt="visitor header" width="1080" height="128" /></td> 
    </tr> 
    <tr> 




    <td> 
     <marquee direction="left" width="99%" behavior="alternate"> 
     <img src="imggallery/images/akoura/akoura_small.jpg" width="119" height="91"> 
     <img src="imggallery/images/ballaa2/ballaa2_small.jpg" width="119" height="91"> 
     <img src="imggallery/images/baalbeck/baalbek_small.jpg" height="92"> 
     <img src="imggallery/images/barouk/cedre_barouk_small.jpg" width="119" height="91"> 
     <img src="imggallery/images/batroun/batroun_small.jpg" width="119" height="91"> 
     <img src="imggallery/images/bchareh/bchareh_small.jpg" width="119" height="91"> 
     <img src="imggallery/images/beiteldin/beiteldine_small.jpg" width="119" height="91"> 
     <img src="imggallery/images/beyrouth/beyrouth_small.jpg" width="119" height="91"> 
     <img src="imggallery/images/beyrouth/beyroyj frm sky/beyrouthfromSky_small.jpg" width="119" height="91"> 
     <img src="imggallery/images/deir el mara/deirelamar_small.jpg" width="119" height="91"> 
     </marquee> 
     <hr /> 
     <h2 class="registerTitle">Registration Fields</h2> 
     <h4 class="registerTitle">Sign Up Today....</h4> 
<!-- <div class="container center"> --> 
     <p style="color:#FF0000" align="center"><?php print("$message")?></p> 
    </td> 

    <tr> 
     <table width="680" border="0" align="center"> 
     <form action="register.php" method="post"> 



     <td> 
     </span><label for="firstname"><span class="Fields">First Name</span> <span class="requiredField">*</span></label></td> 
     <td> 
     <input type="text" name="fname" placeholder="Firstname" /></td> 

     <td><span class="Fields">Last Name</span><span class="requiredField">*</span></label></td> 

     <td><input type="text" name="lname" placeholder="Lastname" /></td> 



    <tr> 
     <td><label for="birthdate" class="Fields">Birth Date      </label></td> 
     <td><input type="date" name="birthdate" value= "YYYY_MM_DD" onfocus="if (this.value == 'YYYY_MM_DD') {this.value = '';}" onblur="if (this.value == '') {this.value = 'YYYY_MM_DD';}" /> 
     <br /></td> 

    </tr> 

    <tr> 
     <td class="Fields"><label for="country">Country</label></td> 
     <td><select name="country" class="select"> 
     <option value="0">-- Select Your Country --</option> 
     <?php countryQuery(); ?> 
     </select></td> 
     <td class="Fields"><label for="specialisation">Specialisation <span class="requiredField">*</span></label></td> 
     <td><select name="specialization" class="select"> 
     <option value="0">-- Select Your Specialization --</option> 

     <?php specializationQuery(); ?> 
     </select></td> 

    </tr> 

    <tr> 
     <td class="Fields"><label for="district">District<span class="requiredField">*</span></label></td> 
     <td><select name="district" class="select"> 
     <option value="0">-- Select Your District --</option> 

     <?php districtQuery(); ?> 
     </select></td> 

     <td class="Fields"><label for="village">Village<span class="requiredField">*</span></label></td> 
     <td><select name="village" class="select"> 
     <option value="0">-- Select Your Village --</option> 

     <?php villageQuery(); ?> 
     </select></td> 

    </tr> 

    <tr> 
     <td class="Fields"><label for="email">Email Adress<span class="requiredField">*</span></label></td> 
     <td><input type="text" name="email" placeholder="Email Adress" /> 
     <br /></td> 
     <td><label for="username"><span class="Fields">User Name</span> <span class="requiredField">*</span></label></td> 
     <td><input type="text" name="username" placeholder="Username" /> 
     <br /></td> 

    </tr> 

    <tr> 
     <td class="Fields"><label for="password">Password<span class="requiredField">*</span></label></td> 
     <td><input type="password" name="pass1" placeholder="Password" /> 
     <br /></td> 
     <td class="Fields"><label for="password2">Re_Password<span class="requiredField">*</span></label></td> 
     <td><input type="password" name="pass2" placeholder="Validate Password" /> 
     <br /></td> 

    </tr> 

    <tr> 
     <td></td> 
     <td></td> 
     <td><input type="submit" value="Register"/></td> 
     <td></td> 
    </tr> 

    </form> 
</table> 
<!--</div>--> 
    </tr> 



    <tr> 
    <td><?php require_once('footer.php'); ?></td> 
    </tr> 
    </tr> 
    </tr> 
</table> 



</body> 
</html> 
<?php ob_end_flush(); ?> 
+2

什麼是$查詢這裏** $ getid = mysql_fetch_array($ query); **? – sbaaaang

+0

您的查詢失敗。總是檢查查詢是否成功,否則你會對這個問題中描述的問題感到好玩。 –

+1

[**請不要在新代碼中使用'mysql_ *'函數**](http://bit.ly/phpmsql)。他們不再被維護[並被正式棄用](https://wiki.php.net/rfc/mysql_deprecation)。看到[**紅框**](http://j.mp/Te9zIL)?學習[*準備的語句*](http://j.mp/T9hLWi),並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [這篇文章](http://j.mp/QEx8IB)將幫助你決定哪個。如果你選擇PDO,[這裏是一個很好的教程](http://j.mp/PoWehJ)。 – h2ooooooo

回答

-1

按照PHP doc例子,我認爲retieving結果,當你丟失somenthing,所以儘量在貴方的查詢,並檢查它;):

<?php 
//= Query ========================// 
$sql=mysql_query("select * from table1"); 

//= Closed while ====================// 
/*everytime it fetches the row, adds it to array...*/ 
while($r[]=mysql_fetch_array($sql)); 

echo "<pre>"; 
//= Prints $r as array =================// 
print_r ($r); 
//=============================// 
echo "</pre>"; 
?> 
1

運行您所有的疑問這樣的,你永遠知道什麼地方出了錯,並責備什麼碼

$res = mysql_query("SELECT * FROM specialization"); 
if (!$res) { 
    throw new Exception(mysql_error()); 
} 
1

嗨,老兄,你已經通過$查詢mysql_fetch_array($查詢), 但是$查詢插入查詢

$query = mysql_query("SELECT * FROM <your table name>"); 

$mysql_fetch_array($query); 

現在它會工作,$ mysql_fetch_array();在選擇你想要獲取記錄的地方工作。

+0

所以如何解決這個問題,我需要做什麼來獲取輸入用戶存儲在變量中以便在會話中使用它? – user2277081

+0

不,你插入的數據沒有提取和mysql_fetch_array()函數使用,當你從數據庫中取代記錄而不是inster –

+0

意味着如果你想從表名學生中選擇數據,然後嘗試SELECT * FROM students –