I’m trying to add some text from a TEXTAREA in HTML to my table in mariaDB using PHP in Linux. Basically the form has two “boxes”. One to identify the user and the second one to add a text. If the user have already added a text, will not be possible to add more text. I am struggling to add the name and the text from TEXTAREA (from HTML) using a query in PHP. Any ideas??
This is my table in MariaDB:
create table TableMarch( id int NOT NULL AUTO_INCREMENT, name varchar(255) UNIQUE, messagetext TEXT, date DATE NOT NULL, PRIMARY KEY (id) );
This is my form in html:
---SOMETHING--- . . . <body> <form action="form_submit.php" class="alt" method="POST"> <div> <img class="marginauto" src="image.JPG" alt="centered image" /> </div> <h2> <center>APP</center></h2> <label for="name">ID</label> <input type="text" name="name" id="name" placeholder="ID"/> <textarea name="message" for="message" placeholder="Please, type your message" maxlength="300"></textarea> <input type="submit" name="submit" value="Submit"/> </form> </body> </html>
and this is my code in PHP:
<?php $host = "localhost"; $db_name = "dev_to"; $username = "XXX"; $password = "XXX"; $connection = null; try{ $connection = new PDO("mysql:host=" . $host . ";dbname=" . $db_name, $username, $password); $connection->exec("set names utf8"); }catch(PDOException $exception){ echo "Connection error: " . $exception->getMessage(); } function saveData($name){ global $connection; $query = "INSERT INTO TableMarch(name, messagetext) VALUES( :name , :messagetext )"; $callToDb = $connection->prepare( $query ); $name=htmlspecialchars(strip_tags($name)); $messagetext = $_POST['messagetext']; //$callToDb->bindParam(":name", $name); $callToDb->bindParam(":name", $name, ":messagetext", $messagetext ); if (!$callToDb->execute()) { $_SESSION['message']='This is your message'; $affected_rows = $callToDb->rowCount(); if ($affected_rows == 0) { $query2 = "SELECT date FROM test WHERE name = ?"; $result2 = $connection->prepare($query2); $result2->execute([$name]); $datee = $result2->fetch(); foreach ($datee as $value); printf('<h1> User ' . $name . ' has already sent a comment: ' . $value . '</h1>' ); echo '<p><a href="http://xx.xx.xx.xx/app.html" title="Return to previous page">« return to app</a></p>'; } } else { printf('<h1> Thanks for sending your comment. </h1>'); echo '<p><a href="http://xx.xx.xx.xx/aplicativo.html" title="Return to previous page">« return to app</a></p>'; } } if( isset($_POST['submit'])){ $name = htmlentities($_POST['name']); //then you can use them in a PHP function. $result = saveData($name); //echo $result; } else{ echo '<h3 style="text-align:center;">ERROR WITH LOGIN</h3>'; } ?>
Advertisement
Answer
Missing comma between values try
INSERT INTO TableMarch(name, messagetext) VALUES( :name , :messagetext)
Also turn on PDO exceptions:
$connection = new PDO("mysql:host=" . $host . ";dbname=" . $db_name, $username, $password); $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
You’ll see an error in your case (or maybe some other)
Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 1364 Field ‘date’ doesn’t have a default value in …
If you’ll allow date
field to be null or always will insert valid date record will be inserted.
Just tested.
Also! Make sure that $_POST['name']
and $_POST['textmessage']
are sent.
As I can see, in form you have name="message"
but trying to fetch textmessage
. Pls fix all these bugs first. Use debugger and browser’s inspector to make sure all required data arrives to the script.