How can I use implode inside query to insert the values into db. here is my example:
Html form :
<form action="insert.php" method="post">
name:<input type="text" name="name" /><br />
address:<input type="text" name="address" /><br />
phone:<input type="text" name="phone" /><br />
<input type="submit" name="insert" value="insert" />
</form>
I use this code to get _POST values from the application form:
$x = array_values($_POST);
and here to put "," comma between the strings so I can use it in the query :
$x = "'".implode("','",$x)."'";
here is what I did:
mysql_query("INSERT INTO dbtable (name, address, phone)
VALUES
($x)");
When I echo $x:
the results is: 'name','address','phone','insert'
the code print "insert"<- it's the submit button. I think this is the problem
can anyone help me to fix my mistake please
Thanks
the implode shuld be like:
$x =implode("','",$x)."'";
Related
i want to use if(isset($_POST['name'])) loop to test if user enter data so it will store it in database and if not so dont store it even when i refresh the page!!
i've tried to use if(isset($_POST['name'])) loop to test if user enter data so it will store it in database and if not so dont store it even when i refresh the page but doesnt work ! and when i refresh the page it store the last data that i've tried before!!
this is index.php :
<!DOCTYPE html>
<html>
<body>
<ul><?php
require "controller/database.php";
require "controller/users.php";
$user=new Users();
$user->insertUser($_POST['name'],$_POST['age'],$_POST['email']);
?></ul>
<form action="" method="POST">
<input type="text" name="name" required placeholder="Name"><br><br>
<input type="number" name="age" required placeholder="Age" ><br><br>
<input type="text" name="email" required placeholder="Email" ><br><br>
<button type="submit" name="insert" value="Add Data To Database"></button>
</form>
</body>
</html>
this is the users.php file :
<?php
class Users extends DB {
function insertUser($name,$age,$email){
$query = "INSERT INTO users (name, age, email )
VALUES ('$name', '$age', '$email')";
$res=$this->insert($query);
//return $this->select("SELECT * FROM `users`");
}
}
i've tried to use if(isset($_POST['name'])) loop to test if user enter data so it will store it in database and if not so dont store it even when i refresh the page but doesnt work ! and when i refresh the page it store the last data that i've tried before!!
Please help!!
it show this error and store the data at the same time !
Notice: Undefined index: name in C:\xampp\htdocs\gestion de paie\index.php on line 10
Notice: Undefined index: age in C:\xampp\htdocs\gestion de paie\index.php on line 10
Notice: Undefined index: email in C:\xampp\htdocs\gestion de paie\index.php on line 10
You can put a condition before you create user, in index file
index.php
<!DOCTYPE html>
<html>
<body>
<ul><?php
require "controller/database.php";
require "controller/users.php";
if(isset($_POST['name']) && isset($_POST['age']) && isset($_POST['email']){
$user=new Users();
$user->insertUser($_POST['name'],$_POST['age'],$_POST['email']);
}
?></ul>
<form action="" method="POST">
<input type="text" name="name" required placeholder="Name"><br><br>
<input type="number" name="age" required placeholder="Age" ><br><br>
<input type="text" name="email" required placeholder="Email" ><br><br>
<button type="submit" name="insert" value="Add Data To Database"></button>
</form>
</body>
</html>
Also don't put user inputs inside a query without sanitize, i suggest always to controll user inputs before you enter in you sql query:
users.php :
<?php
class Users extends DB {
function insertUser($name,$age,$email){
// Sanitize your data always don't trust user
$name = mysql_real_escape_string($name);
$age = mysql_real_escape_string($age);
$email = mysql_real_escape_string($email);
$query = "INSERT INTO users (name, age, email )
VALUES ('$name', '$age', '$email')";
$res=$this->insert($query); //return $this->select("SELECT * FROM `users`");
Hope to be the needed question :)
Instead of isset, you should use !empty (look here), as isset doesn't work for arrays. isset($_POST["anything"]) always returns true, while !empty($_POST["anything"]) only returns true if $_POST["anything"] exists.
<!DOCTYPE html>
<html>
<body>
<ul><?php
require "controller/database.php";
require "controller/users.php";
if (!empty($_POST['name']) && !empty($_POST['age']) && !empty($_POST['email']) {
$user=new Users();
$user->insertUser($_POST['name'],$_POST['age'],$_POST['email']);
}
?></ul>
<form action="" method="POST">
<input type="text" name="name" required placeholder="Name"><br><br>
<input type="number" name="age" required placeholder="Age" ><br><br>
<input type="text" name="email" required placeholder="Email" ><br><br>
<button type="submit" name="insert" value="Add Data To Database"></button>
</form>
</body>
</html>
Also, you should use Prepared statements.
I am looking how to reuse the value="typed_from_user" into another tag in the same formulary without using js, just PHP & HTML.
Is it even possible?
For example, here i want to reuse the word 'pizza'
<form>
<input name="hello" type="text" value="I want pizza">
<input name="order" type="text" value="pizza">
</form>
maybe something storing it in a variable?
<form>
<input name="hello" type="text" value="<?php echo $whatyouwant ;?>">
<input name="lunch" type="text" value="<?php echo substr($whatyouwant,7,5);?>"><!--cut it from letter 7, the word pizza-->
<input name="order" type="submit" value="submit">
</form>
yeah, supouse that we know where exactly starts the word pizza
This are the files i am using as tests
action.php
form.php
<?php
//action.php
$var = $_POST['hello'];
$var_lunch= $_POST['lunch'];
echo "hello: $var<br>";
echo "lunch: $var_lunch<br>";
?>
file form.php:
<html>
<header>
<title>test-page</title>
<!--bootstrap-->
<link rel="stylesheet" type="text/css" href="../../../css/bootstrap/bootstrap_v4.0.0.css">
</header>
<body>
<?php
$whatyouwant=777;
?>
<form method="post" action="action.php">
<input name="hello" type="text" value="<?php echo $whatyouwant ;?>">
<input name="lunch" type="text" value="<?php echo substr($whatyouwant,7,5);?>">
<input name="order" type="submit" value="submit">
</form>
</body>
</html>
OUTPUT:
hello: i want pizza
lunch:
If you are trying to get the input value form one text field to another text field only using PhP and HTML, I have to say that it is quite impossible. PhP is the server side language so to get the value from one field to another, it must first reach the server and then it display but will show error in PhP.
To achieve this, you have to use JS. You can achieve this from just a few lines of code using jQuery.
I'm making a website where users can insert data into a database and keep track of their medicine intake.
I have a simple form that submits to an SQL database:
<form method='post'>
<b>Dato:</b> <input type="date" name='date' id='date' value="<?php echo date('Y-m-d'); ?>" /><br /><br \>
<b>Tid:</b> <input type="time" name='time' id='time' value="<?php echo " $hour:$minute "; ?>" /><br /><br \>
<b>Medicin:</b><br /> <input type="radio" name="medicin" value="Medicin1" checked> Medicin1 <br />
<input type="radio" name="medicin" value="medicin2"> Medicin2 <br />
<input type="radio" name="medicin" value="other"> Other <input type="text" name="otherMedicin"><br /><br />
<b>Kommentar:</b><br /><textarea name='comment' id='comment'></textarea><br /> <hr \>
<input type='submit' value='Send' />
</form>
To submit it to the database I found a previous question about the same thing that I follow. Unfortunately I just can't get it to work!
html radio button with an "other" selection that has a text box
Here is the important part of my code:
$users_date = mysqli_real_escape_string($con, $_POST['date']);
$users_time = mysqli_real_escape_string($con, $_POST['time']);
$users_medicin = mysqli_real_escape_string($con, $_POST['medicin']);
$otherMedicin = mysqli_real_escape_string($db, $_POST['otherMedicin']);
if ($users_medicin == 'otherMedicin') {
$medicinField = $otherMedicin;
} else {
$medicinField = $users_medicin;
}
$users_comment = mysqli_real_escape_string($con, $_POST['comment']);
$query = "
INSERT INTO `myDatabase`.`medi_list` (`id`, `dato`, `tid`, `medicin`, `kommentar`) VALUES (NULL, '$users_date',
'$users_time', '$medicinField', '$users_comment');";
I'm still quite new to SQL so I'd appreciate any help you can throw my way :)
I see some basic problems in your code:
When you are populating the field "$otherMedicin", you are using $db instead of $con like the other statements.
$_POST[medicin] is NEVER going to be "otherMedicin", because that is the name of your text input field, not the radio button name. You should have this as your conditional:
if ($users_medicin == 'other') {
That should help a bit.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Headers already sent by PHP
I have a database and when I sumit info, i get this error:
"Warning: Cannot modify header information - headers already sent by (output started at /Applications/XAMPP/xamppfiles/htdocs/mostra/html/noticias/publicacomentario.php:2) in /Applications/XAMPP/xamppfiles/htdocs/mostra/html/noticias/publicacomentario.php on line 14"
I would like rest in the same page When I insert values to database, this my code:
VER.PHP
<form action="noticias/publicacomentario.php" method="post" id="commentform" onsubmit="MM_validateForm('usuario','','R','email','','RisEmail','comentario','','R');return document.MM_returnValue">
<fieldset>
<input type="hidden" name="noticia_id" value="<?php echo $id; ?>"><br>
<p><label>NOMBRE *</label>
<input type="text" name="usuario"></p>
<p><label for="email">EMAIL (No se publicará) *</label>
<input type="text" name="email"></p>
<p><label for="comment">COMENTARIO</label>
<textarea name="comentario" id="comment" cols="100%" rows="10" tabindex="4"></textarea></p>
<p><input type="submit" name="submit" id="submit" tabindex="5" value="Enviar " /></p>
</fieldset>
</form>
PUBLICARCOMENTARIO.PHP
<?php
require ('connect.php');
$id=$_POST['noticia_id'];
$nick=$_POST['usuario'];
$email=$_POST['email'];
$comentario=$_POST['comentario'];
$query = "INSERT INTO comentarios (usuario,email,comentario,noticia_id, fecha) VALUES('$nick','$email','$comentario','$id', NOW())";
mysql_query($query) or die(mysql_error());
$query = "UPDATE noticias SET num_comentarios= num_comentarios+1 where id_noticia='".$id."'";
mysql_query($query) or die(mysql_error());
header("Location: ver.php?id=$id")
?>
Any idea? thx!
It says line 2, but there's nothing actually on line 2 of PUBLICARCOMENTARIO.PHP.
Ensure that you haven't got any spaces or new lines above "PUBLICARCOMENTARIO.PHP" before the PHP opening tag.
If PUBLICARCOMENTARIO.PHP only contains PHP (No HTML), then you shouldn't require the closing ?> either.
Basically the error is saying that the header("xxx") can't be output as you've output something else already. Usually it's some HTML or some space before the opening PHP tag.
you should remove blank lines before "<?php" in PUBLICARCOMENTARIO.PHP
Check if PUBLICARCOMENTARIO.PHP is saved as UTF8+BOM and if yes, remove the BOM.
You can do this with Notepad++ or any other good editor.
I put the PUBLICARCOMENTARIO.PHP into VER.PHP
<form action="" method="post" id="commentform" onsubmit="MM_validateForm('usuario','','R','email','','RisEmail','comentario','','R');return document.MM_returnValue">
<fieldset>
<input type="hidden" name="noticia_id" value="<?php echo $id; ?>"><br>
<p><label>NOMBRE *</label>
<input type="text" name="usuario"></p>
<p><label for="email">EMAIL (No se publicará) *</label>
<input type="text" name="email"></p>
<p><label for="comment">COMENTARIO</label>
<textarea name="comentario" id="comment" cols="100%" rows="10" tabindex="4"></textarea></p>
<p><input type="submit" name="submit" id="submit" tabindex="5" value="Enviar " /></p>
</fieldset>
</form>
</div>
<?php
if ($_POST) {
require ('connect.php');
$id=$_POST['noticia_id'];
$nick=$_POST['usuario'];
$email=$_POST['email'];
$comentario=$_POST['comentario'];
$query = "INSERT INTO comentarios (usuario,email,comentario,noticia_id, fecha) VALUES('$nick','$email','$comentario','$id', NOW())";
mysql_query($query) or die(mysql_error());
$query = "UPDATE noticias SET num_comentarios= num_comentarios+1 where id_noticia='".$id."'";
mysql_query($query) or die(mysql_error());
}
header("Location: $_SERVER[PHP_SELF]?id=$id");
?>
Inside the HEADER("location: ....) i put the $_SERVER[PHP_SELF].
With this works.. but the values entered are not displayed until you refresh the page.. why??
I have a text field
<form method="post">
<input type="text" name="number">
<input type="submit" value="submit" name="ok">
</form>
I want to enter a integer in this text field and want to open other text field(s) equal to number enter in that text field.
eg. I enter 4 number in the $number then the 4 text fields will open.
the code I used for this.
<?php
if(isset($_POST['ok']))
{
extract($_POST);
for($i=1;$i<=$number;i++)
{
<input type="text" name="">
//insert mysql query to insert into table
}
}
?>
but i don't know how to insert these values in the table.
Try like this
<?php
if(isset($_POST['ok']))
{
extract($_POST);
for($i=1;$i<=$number;i++)
{
<input type="text" name="text[]">
//insert mysql query to insert into table
}
//Here you insert the text field values with name "text" which is an array.You can implode them with ',' and insert into your database
}
?>
try with this code
//HTML CODE
<input type="text" name="range" id="range" />
<input type="button" onclick="insertInputBox()" value="Insert TextBox" />
<div id="inputHolder">
</div>
//JAVASCRIPT
function insertInputBox()
{
var rangeLimit=document.getElementById('range').value;
var i=1;
for(i=1;i<=rangeLimit;i++)
{
document.getElementById('inputHolder').innerHTML+='<input type="text" name=i+"range" id=i+"range" /><br />';
}
}