0

I need some help I am trying to create a PHP form using sqlite3 database. I am looking up values from from an existing sqlite3 database in the table2 where the column id = 340 and display those values as a dropdown selection. Then once the value is selected by the user then the form is submitted by the users which updates the new values to the table1 with the values from the php form. I get it to display the names in the dropdown but when I click on the update button to submit the data it updates what the value is in the array.

For example lets say I have 3 fruits in the table and I select pear it updates the table with a "1" instead of the word "pear"

apple pear peach

PHP entry page Code:

<html>
<head>
<title></title>
</head>
<div class = "controlbox">
<body style="font-size:12;font-family:verdana">
<form action="post.php" method="post">
<p> 
<h1>  </h1>
<br>
<br>
Slot1 : <select name="slot1">
<option>--Available Options--</option>
<?php
try
{
$db = new PDO("sqlite:DefaultLibrary.db");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);   
}
catch(Exception $e)
{   
echo $e->getMessage();
}

$stmt2 = $db->query ("SELECT * FROM table2 where ID = '340' ");
$rowarray = $stmt2->fetchall(PDO::FETCH_ASSOC);
$slot1 = 0;

foreach($rowarray as $row)
{
 echo "<option value = $slot1 >$row[FirstName] $row[LastName]</option>";
 $slot1++;
}
?>

</select><br>

<p>
<input type="submit" name="update" value="update">
</p>

</form>
</body>
</html>

PHP Code: Post.php

<?php

$slot1 = sqlite_escape_string($_POST['slot1']);

try
{
 $db = new PDO("sqlite:DefaultLibrary.db");
 $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);  
}
catch(Exception $e)
{   
echo $e->getMessage();
}

if (!empty($slot1)) {
try
{   
    $stmt = $db->prepare("UPDATE table1 SET Slot1place = :slot1 WHERE ID = '340'");
    $stmt->bindParam(':slot1', $slot1,PDO::PARAM_STR);
    $stmt->execute();
}
catch(Exception $e)
{
    echo $e->getMessage();
}

    echo "submitted successfully";


    }
    ?>
0

3 Answers 3

1

You dont use sqlite_escape_string if youre using a prepared statement like that. The values are going to be quoted witn they are bound to the statement.

Sign up to request clarification or add additional context in comments.

Comments

0

I think you should check your html syntax (Is it missing tags, and the ). Check it out at: http://www.w3schools.com/html5/tag_option.asp

Comments

0
 echo "<option name = $name >$row[FirstName] $row[LastName]</option>";

Everything else is the right syntax

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.