2017-03-15 65 views
0

有人可以幫我解決這個問題嗎? 我有一個3 INT列(danni,lenni,anders)表。我希望這個表單插入一個新行,並在每次使用時在字段中寫入數字。它確實插入了一個新行,但無論輸入什麼數字,只在表中插入「0」。通過HTML INSERT INTO

<?php 
// Connection 

// Check connection 
if($link === false){ 
die("ERROR: Could not connect. " . mysqli_connect_error()); 
} 

// Escape user inputs for security 
$danni = mysqli_real_escape_string($link, $_REQUEST['danni']); 
$lenni = mysqli_real_escape_string($link, $_REQUEST['lenni']); 
$anders = mysqli_real_escape_string($link, $_REQUEST['anders']); 

// attempt insert query execution 
$sql = "INSERT INTO ligabs3 (danni, lenni, anders) VALUES ('$danni', '$lenni', '$anders')"; 
if(mysqli_query($link, $sql)){ 
echo "Records added successfully."; 
} else{ 
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link); 
} 

// close connection 
mysqli_close($link); 
?> 

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<title>Add Records Form</title> 
</head> 
<body> 
<form action="<?=$_SERVER['PHP_SELF']?>" method="post"> 
<p> 
<label for="danni">Danni:</label> 
<input type="text" name="danni" id="danni"> 
</p> 
<p> 
<label for="lenni">Lenni:</label> 
<input type="text" name="lenni" id="lenni"> 
</p> 
<p> 
<label for="anders">Anders:</label> 
<input type="text" name="anders" id="anders"> 
</p> 
<input type="submit" value="Add Records"> 
</form> 
</body> 
</html> 
+1

瞭解準備好的發言 – Jens

+0

'$ _REQUEST'必須是'$ _POST' – Jens

+0

你在一個文件中使用此,所以它的自動只要你加載它就插入0。使用'!empty()'/'isset()'。 –

回答

0
there are so many errors in your code. when you refresh page code will execute and insert in database. not after submit form. everytime on page refresh it will insert. 
    name is compulsory for submit button. 
try this. 
    <?php 
    if($_POST['addrecord']){ 
    // Connection 

    // Check connection 
    if($link === false){ 
    die("ERROR: Could not connect. " . mysqli_connect_error()); 
    } 

    // Escape user inputs for security 
    $danni = mysqli_real_escape_string($link, $_REQUEST['danni']); 
    $lenni = mysqli_real_escape_string($link, $_REQUEST['lenni']); 
    $anders = mysqli_real_escape_string($link, $_REQUEST['anders']); 

    // attempt insert query execution 
    $sql = "INSERT INTO ligabs3 (danni, lenni, anders) VALUES ('$danni', '$lenni', '$anders')"; 
    if(mysqli_query($link, $sql)){ 
    echo "Records added successfully."; 
    } else{ 
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link); 
    } 

    // close connection 
    mysqli_close($link); 
    } 
    ?> 

    <!DOCTYPE html> 
    <html lang="en"> 
    <head> 
    <meta charset="UTF-8"> 
    <title>Add Records Form</title> 
    </head> 
    <body> 
    <form action="<?=$_SERVER['PHP_SELF']?>" method="post"> 
    <p> 
    <label for="danni">Danni:</label> 
    <input type="text" name="danni" id="danni"> 
    </p> 
    <p> 
    <label for="lenni">Lenni:</label> 
    <input type="text" name="lenni" id="lenni"> 
    </p> 
    <p> 
    <label for="anders">Anders:</label> 
    <input type="text" name="anders" id="anders"> 
    </p> 
    <input type="submit" name="addrecord" value="Add Records"> 
    </form> 
    </body> 
    </html> 
0

試試這個代碼...

<?php 
if($_POST['addNewRecords']){ 

if($link === false){ 
die("ERROR: Could not connect. " . mysqli_connect_error()); 
} 


$danni = mysqli_real_escape_string($link, $_POST['danni']); 
$lenni = mysqli_real_escape_string($link, $_POST['lenni']); 
$anders = mysqli_real_escape_string($link, $_POST['anders']); 


$sql = "INSERT INTO ligabs3 (danni, lenni, anders) VALUES ('$danni', '$lenni', '$anders')"; 
if(mysqli_query($link, $sql)){ 
echo "Records added successfully."; 
} else{ 
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link); 
} 


mysqli_close($link); 
} 
?> 

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<title>Add Records Form</title> 
</head> 
<body> 
<form action="<?=$_SERVER['PHP_SELF']?>" method="POST"> 
<p> 
<label for="danni">Danni:</label> 
<input type="text" name="danni" id="danni"> 
</p> 
<p> 
<label for="lenni">Lenni:</label> 
<input type="text" name="lenni" id="lenni"> 
</p> 
<p> 
<label for="anders">Anders:</label> 
<input type="text" name="anders" id="anders"> 
</p> 
<input type="submit" name="addNewRecords" value="Add Records"> 
</form> 
</body> 
</html>