2017-10-17 64 views
-1

好吧,我正在做一些工作在PHP和SQL。這個php代碼不會工作,我需要知道如果這個if else循環做什麼。'其他'(T_ELSE)在你的代碼行21

我得到這個錯誤:解析錯誤:行中的「我的文件路徑」語法錯誤,意想不到的「其他」(T_ELSE)21

<?php 
$debug = true; 

# Shows the records in prints 
function show_records($dbc) { 
    # Create a query to get the name and price sorted by price 
    $query = 'SELECT num, fname, lname FROM presidents ORDER BY num DESC' ; 

    # Execute the query 
    $results = mysqli_query($dbc , $query) ; 
    check_results($results) ; 
} 

function valid_number($dbc, $num, $fname, $lname){ 

    if(!valid_number($num)) { 
    echo "<p>Please give a valid number.</p>"; } 
    else { 
     if (!valid_name($fname)) 
    echo "<p>Please complete the first name.</p>"; } 
    else{ 
     if (!valid_name($lname)){ 
    echo "<p>Please complete the last name.</p>"; }} 
    else { 
    insert_record($dbc,$num,$fname,$lname) ; } 
    function valid_number($num){ 
    { if(empty($num) || !is_numeric($num)) } 
    return false ; } 
    else { 
     $num = intval($num) ;  
     if($num <= 0){ 
     return false ;} 
    } 
    return true; } 


function valid_name($fname, $lname){ 
     if(!empty($fname) && !empty($lname)) { 
     return false; 
    } 
    else 
     return true; 
} 



    # Show results 
if($results) 
    { 
     # But...wait until we know the query succeeded before 
     # starting the table. 
     echo '<H1>Presidents</H1>' ; 
     echo '<TABLE border="1\"">'; 
     echo '<TR>'; 
     echo '<TH>Number</TH>'; 
     echo '<TH>First Name</TH>'; 
     echo '<TH>Last Name</TH>'; 
     echo '</TR>'; 

    # For each row result, generate a table row 
     while ($row = mysqli_fetch_array($results , MYSQLI_ASSOC)) 
    { 
     echo '<TR>' ; 
     echo '<TD>' . $row['num'] . '</TD>' ; 
     echo '<TD>' . $row['fname'] . '</TD>' ; 
     echo '<TD>' . $row['lname'] . '</TD>' ; 
     echo '</TR>' ; 
    } 

    # End the table 
    echo '</TABLE>'; 

    # Free up the results in memory 
    mysqli_free_result($results) ; 
} 


# Inserts a record into the prints table 
function insert_record($dbc, $num, $fname, $lname) { 
    $query = 'INSERT INTO presidents(num, fname, lname) VALUES (' . $num . ' , "' . $fname . '", "' . $lname . '")' ; 
    show_query($query); 

    $results = mysqli_query($dbc,$query) ; 
    check_results($results) ; 

    return $results ; 
} 

# Shows the query as a debugging aid 
function show_query($query) { 
    global $debug; 

    if($debug) 
    echo "<p>Query = $query</p>" ; 
} 

# Checks the query results as a debugging aid 
function check_results($results) { 
    global $dbc; 

    if($results != true) 
    echo '<p>SQL ERROR = ' . mysqli_error($dbc) . '</p>' ; 
} 
?> 

確定它說我的文章主要是代碼,所以這裏的一些填料。 lorem ipsum dolor坐amet請忽略這我只需要一個答案這是填充呃uhhhhh是

+0

'if(!valid_name($ fname))'不打開新塊。即沒有'{'。但是你試圖在下一行關閉它。這實際上關閉了之前的else塊並導致錯誤。 – jh1711

+1

請縮進你的代碼。你的21號線在哪裏?檢查你是否在這一行,我認爲你錯過了一個'{' – sheplu

+0

@ jh1711這真的工作,謝謝。我似乎也得到了一個錯誤,其中結果變量未定義,任何想法? – jwsqljava

回答

0

在這裏,請注意這是未經測試,但我可能已經解決了這個問題。您應該嘗試使用elseif(){}並觀察開始和結束括號的位置

<?php 
$debug = true; 

# Shows the records in prints 
function show_records($dbc) { 
    # Create a query to get the name and price sorted by price 
    $query = 'SELECT num, fname, lname FROM presidents ORDER BY num DESC' ; 

    # Execute the query 
    $results = mysqli_query($dbc , $query) ; 
    check_results($results) ; 
} 

function valid_number($dbc, $num, $fname, $lname){ 

    if(!valid_number($num)) { 
     echo "<p>Please give a valid number.</p>"; } 
    elseif(!valid_name($fname)) { 

     echo "<p>Please complete the first name.</p>"; } 
    elseif (!valid_name($lname)){ 
     { 
      echo "<p>Please complete the last name.</p>"; }} 
    else { 
     insert_record($dbc,$num,$fname,$lname) ; } 


    function valid_number($num){ 
     if(empty($num) || !is_numeric($num)){ 
     return false ; } 
    else { 
     $num = intval($num) ; 
     if($num <= 0){ 
      return false ;} 
    } 
    return true; 
    } 
相關問題