MuhammadJavadi
MuhammadJavadi

Reputation: 33

Value of 1 is stored In database instead of the text Box value / PHP-MySql

Well i want to store 3 textbox values into my mysqli database, after running these codes nothings happen! and if anything happen then number '1' store into database instead Textboxes Value

PHP Code:

<?php
include 'displaycustomersdb.php'; //this file give $Ccode from user sessions and config.php is in it.

global $textareamsg;
global $title;
global $reciver;
global $sender;

$textareamsg = (isset($_GET['textarea']));
$title = (isset($_GET['title']));
$reciver = (isset($_GET['reciver']));

if (isset($_GET['sendmsg'])) {
    $sql_msg = mysqli_query($link, 'INSERT INTO messaging (title, message, sender, reciver) VALUES (' . $title . ',' . $textareamsg . ',' . $Ccode . ',' . $reciver . '');
}
?>

HTML Code:

<?php include 'sendmsg.php'; ?>
<form method="get">
  <i class="fas fa-envelope-open-text"></i><input type="text" placeholder="Ttitle here ..." name="title">
  <br>
  <br>
  <i class="fas fa-user-plus"></i><input type="text" placeholder="TO" name="reciver">
  <br>
  <br>
  <textarea rows="10" cols="100" placeholder="textarea" name="textarea" style="padding:10px;"></textarea>
  <br>
  <br>
  <button type="submit" name="sendmsg"><i class="far fa-share-square"></i>Insert</button>
</form>

Upvotes: 0

Views: 56

Answers (3)

MuhammadJavadi
MuhammadJavadi

Reputation: 33

Well, thank you everyone. i did try this and it's working:

$textareamsg = $_GET['textarea'];
$title      =  $_GET['title'];
$reciver    =  $_GET['reciver'];

if (isset($_GET['sendmsg'])) {
    $sql_msg = mysqli_query($link, "INSERT INTO messaging (title, message, sender, reciver) VALUES ('$title','$textareamsg', '$ccode','$reciver')");
}

Upvotes: 1

Qrazier
Qrazier

Reputation: 182

I think your single quotes are messing up your query syntax.
As of here, your values must be quoted in PHP like such

$sql_msg = mysqli_query($link, "INSERT INTO messaging (title, message, sender, reciver) VALUES ('$title', '$textareamsg', '$Ccode', '$reciver')";

and if anything happen then number '1' store into database instead Textboxes Value

I don't quite understand what are you trying to achieve here, but I think this is true?

if (isset($_GET['textarea'])) $textareamsg = $_GET['textarea'];
else $textareamsg = 1;

if (isset($_GET['title'])) $title = $_GET['title'];
else $title = 1;

if (isset($_GET['reciver'])) $reciver = $_GET['reciver'];
else $reciver = 1;

Sorry, I misunderstood your question. As @ajafari pointed out,

isset() return true if the given value exists and it store in your DB as 1

Upvotes: 1

ajafari
ajafari

Reputation: 193

You get boolean values instead of the values itself. Remember isset() return true if the given value exists and it store in your DB as 1. I guess you intend to check if value is exists, whitch you can do it like below:

$textareamsg = isset($_GET['textarea'])? $_GET['textarea'] : '';
$title = isset($_GET['title'])? $_GET['title'] : '';
$reciver = isset($_GET['reciver'])? $_GET['reciver']: '';

And also you should store string values like this:

if (isset($_GET['sendmsg'])) {
    $sql_msg = mysqli_query($link, 'INSERT INTO messaging (title, message, sender, reciver) VALUES ("' . $title . '","' . $textareamsg . '","' . $Ccode . '","' . $reciver . '"');
}

Hope it will be helpful :)

Upvotes: 3

Related Questions