I just have rewritten my mysql code to PDO, but I just don’t get it working. I’ve been debugging, but just don’t seem te be getting it right. It is all new for me, but it connect tot the database, it reads from the database in my vardump, but it won’t post in the textarea, or even echo for that matter. Let alone, writing to the database… I’ve got a gut feeling that is it the $result var that is doing some weird stuff, but I doublechecked everything as far as I know.
<html>
<head>
<title>Milan CMS</title>
</head>
<body>
<?php
$host= "";
$dbname= "";
$username= "";
$password= "";
$dbh = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$sth = $dbh->prepare("SELECT * FROM milan WHERE tag='trainingen'");
$sth->execute();
$result = $sth->fetchAll();
var_dump($result);
echo $result['value'];
echo $result;
?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" name="trainingen">
Trainingen:<textarea name="nametrainingen" rows="10" cols="60"><?php echo $result['value']; ?></textarea></p>
<input name="submit" type="submit" value="Verzenden"><input type="reset" value="Veld leeg maken">
</form>
<?php
$invoer = $_POST['nametrainingen'];
if (isset($_POST['submit'])) { //test if submit button is clicked
$sql = "DELETE FROM milan WHERE tag = 'trainingen'";
$sql = "INSERT INTO milan (tag, value) VALUES ('trainingen', '$result')";
$sth->execute();
$result = $sth->fetchAll();
$myFile = "trainingen.json";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, json_encode($result));
fclose($fh);}
?>
<p>
</body>
</html>
Output of the var_dump:
array(1) {
[0]=> array(6) {
["id"]=> string(1) "0"
[0]=> string(1) "0"
["tag"]=> string(10) "trainingen"
[1]=> string(10) "trainingen"
["value"]=> string(17) "Hgioejigojerigjer"
[2]=> string(17) "Hgioejigojerigjer"
}
}
That’s just going to re-run the previous contents of
$sth– you’ll need to prepare those two statements and then run them. You need to run them like this:Editted to add:
$resultends up with an array of values – you’ll have to step through each item in the array.