<?php
$conn=mysqli_connect("localhost","root","","fesdb");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
die();
}
if (isset($_POST['Insert']))
{
$txt = $_POST['text1'];
}
{
$query = "insert into table (Mytext) VALUES ('$txt')";
$result = mysqli_query($conn,$query);
if($result)
{
echo '<script>alert("record inserted")</script>';
}
else
{
echo ' Please Check Your Query ';
}
}
?>
<html>
<body>
<form action="" id="myForm" >
<input type='text' name="text1"><br>
<input type='text' name="text2"><br>
<input type='text' name="text3"><br>
<input type='text' name="text4"><br>
<input type='text' name="text5"><br>
<input type='text' name="text6"><br>
<input type='text' name="text7"><br>
<input type='text' name="text8"><br>
<input type='text' name="text9"><br>
<input type='text' name="text10"><br>
<input type='submit' name="Insert">
</form>
</body>
</html>
I am a beginner in PHP .. I have sql database with one Table , this table have one column .... I have 10 textboxes in my page with one button ... I want to insert each textbox into the table as separated record ... I know I should use loop (for each ...) to solve this problem ... but I dont know how do that ...
if possible help me with a sample
Related
In my first page I have this code:
$number="1234567891";
$str="456";
echo "<form action='edit.php' method='POST'><input type='hidden' name='msg' value='$message' />
<input type='hidden' name='text' value='$number' />
<input type='hidden' name='edit' value='$str' />
<input type='submit' name='chedit' value='Go' style='position:relative; top:25px; left: 50%;'>
</form>";
In my edit.php I have this code:
<form action="#" method="POST">
Edit Number
<input type="text" name="change" value="$mumu"/>
<input type="submit" name="pch" value="Change"/>
</form>
<?php
if (isset($_POST["chedit"]))
{
$suj = $_POST["msg"];
$text = $_POST["text"];
$mumu =$_POST["edit"];
if(isset($_POST["pch"]))
{
$change = $_POST["change"];
$obinna = str_replace("$change","$mumu","$text");
echo $obinna;
}
}
?>
My problem is that whenever I put a new text in new form and click submit to edit a character in the old string submitted line the page refreshes and no result is output. Please can anybody sort this out?
// try this ..
if (isset($_POST["chedit"]))
{
$suj = $_POST["msg"];
$text = $_POST["text"];
$mumu =$_POST["edit"];
if(isset($_POST["pch"]))
{
$change = $_POST["change"];
//456 , //555(post value) , //(your text)12345678910
// $obinna = str_replace("Set old value you change in text","set new value you want to set in text ","your orignal text ");
$obinna = str_replace("$mumu","$change","$text");
echo $obinna;
}
}
echo '<form action="#" method="POST">
Edit Number
<input type="text" name="change" value="" placeholder="Change"/>
<input type="text" name="edit" value="" placeholder="Edit"/>
<input type="submit" name="pch" value="Submit"/>
</form>'
Check Demo Url :- https://eval.in/931366
This is process_upcategory.php
I want to update the category name or the category id with another category name/id by its category id or by its category name.
I'm new to php
<?php
require('includes/config.php');
if(!empty($_POST))
{
$msg=array();
if(empty($_POST['cat']))
{
$msg[]="Please full fill all requirement";
}
if(!empty($msg))
{
echo '<b>Error:-</b><br>';
foreach($msg as $k)
{
echo '<li>'.$k;
}
}
else
{
$cat_nm=$_POST['cat[0]'];
$cat_id=$_POST['cat[1]'];
$query= "UPDATE `category` SET cat_nm='$cat_nm' WHERE cat_id='$cat_id'";
mysqli_query($conn,$query) or die("can't Execute...");
mysql_close($link);
header("location:category.php");
}
}
else
{
header("location:index.php");
}
?>
Now this is category.php, just a snippet of code. Not whole code
<form action='process_upcategory.php' method='POST'>
<b style="color:darkgreen">UPDATE CATEGORY </b> <br>
<b style="color:darkgreen">Old Category</b>
<br>
<select name="cat[]" multiple>
<?php
$query="select * from category ";
$res=mysqli_query($conn,$query);
while($row=mysqli_fetch_assoc($res))
{
echo "<option>".$row['cat_nm'];
echo "<option>".$row['cat_id'];
}
?>
</select>
<br>
<b style="color:darkgreen">New Category</b><br>
<input type='text' name='cat[0]'></input><br>
<input type='text' name='cat[1]'></input>
<input type='submit' value=' UPDATE '>
</form>
I want to update the category name with another category name by its category id or by its category name. I get undefined index cat[0] and cat[1]
When you end an input name with [] it wil be converted to an array by php. The correct way to get the values in this case would be something like this:
$cat=$_POST['cat'];
$cat_nm=$cat[0];
$cat_id=$cat[1];
I combined the two routines into one script.
I added 'sub' to the form to distinguish from when the form was submitted or not.
I used list() in the query result loop.Used mysqli_fetch_array($result, MYSQLI_NUM) rather than mysqli_fetch_assoc($res)
used foreach() to loop through the $_POST['cat']
Added 'value' to the <option value=""> to hold the id
Eliminated the switching from HTML mode to PHP mode by using HEREDOC.
<?php
if (intval($_POST['sub']) == 1){
$newcat = $_POST['new'];
foreach($_POST['cat'] as $key=>$value){
if(strlen($newcat[$key]) > 0){
mysqli_query($conn,"UPDATE `category` SET `cat_nm`='$newcat[$key]' WHERE `cat_id`='$value'");
}
}
}
echo <<<EOT
<html><head><style>h4,h3{color:darkgreen;margin:.2em;}</style></head><body>
<form action="#" method='POST'>
<h3>UPDATE CATEGORY</h3>
<h4>Old Category</h3>
<select name="cat[]" multiple>
EOT;
$sql="SELECT `cat_nm`, `cat_id` FROM `category` ";
$result=mysqli_query($conn,$sql);
while(list($cat_nm,$cat_id) = mysqli_fetch_array($result, MYSQLI_NUM)){
echo " <option value=\"$cat_id\">$cat_nm</option>\n";
}
echo <<<EOT
</select>
<h3>New Category</h3>
<input type="text" name="new[0]" /><br/>
<input type="text" name="new[1]" /><br/>
<input type="hidden" name="sub" value="1" /><br/>
<input type="submit" value=" UPDATE />
</form>
</body></html>
EOT;
?>
I have the following code, the variable 'name' and 'project' need to go to session variables on submit, in this case the submit happens through an 'onchange'event. The 'name' variable is POSTed but not the 'project' variable. Any ideas where my issue is?
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<?php
$query = $conn->prepare("SELECT name, project FROM models.models where models.active = 'yes'");
$query->execute();
while($r = $query->fetch(PDO::FETCH_OBJ)){
echo "
<div class='swiper-slide'>
<input type='image' src='models/thumbs/",$r->name,".jpg' id='name' name='name' value='",$r->name,"' onchange='this.form.submit();'/>
<input type='hidden' value='",$r->project,"' name='project' />
</div>
";
}
?>
</form>
<?php
$query = $conn->prepare("SELECT name, project FROM models.models where models.active = 'yes'");
$query->execute();
while($r = $query->fetch(PDO::FETCH_OBJ)){
echo "
<div class='swiper-slide'>
<input type='image' src='models/thumbs/",$r->name,".jpg' id='name' name='name[]' value='",$r->name,"' onchange='this.form.submit();'/>
<input type='hidden' value='",$r->project,"' name='project' />
</div>
";
}
?>
How to access:
$names = $_POST['name'];
for($i=0;$i<count($names);$i++)
{
echo $names[$i]."<br>";
}
I'm trying to display some information stored in MySQL comments table to an input but I'm having issues with that. Input named enterComment inserts data to my DB and I want it to redirect back to showComment input.
HTML:
form action='takedata.php' method='POST'>
<input type='text' id='enterComment' name='enterComment' width='400px' placeholder='Write a comment...'>
<input type='submit' id='combuton' name='comButon' value='Comment!'>
<input type='text' id='showComment' name='showComment'>
</form>
PHP:
<?php include "mysql.php"; ?>
<?php
if (isset($_POST['comButon'])){
$enterComment = strip_tags($_POST['enterComment']);
if($enterComment){
$addComment = mysql_query("INSERT INTO comments(comment) VALUES('$enterComment')");
if($addComment==1)
//INSERT INTO showComment input;
}
}
?>
try this, and use mysqli instead of mysql
include "dbconnect.php";
if (isset($_POST['comButon'])){
$enterComment = strip_tags($_POST['enterComment']);
if($enterComment){
$addComment = mysqli_query($conn, "INSERT INTO comments(comment) VALUES('$enterComment')");
if($addComment) {
$sql = "select comment from comments order by id desc limit 1";
$result = mysqli_query($conn, $sql);
while($row = $result->fetch_assoc()) { ?>
<input type="text" value="<?php echo $row['comment']; ?>">
<?php }
}
}
}
your form
<form action='' method='POST'>
<input type='text' id='enterComment' name='enterComment' width='400px' placeholder='Write a comment...'>
<input type='submit' id='combuton' name='comButon' value='Comment!'>
<?php if(!isset($_POST['comButon'])) { ?>
<input type="text" value="">
<?php } ?>
</form>
I am a complete beginner, so please bear with me. This is just a test project I am putting together to try to teach myself some of the basics.
I know that a lot of my commands are outdated and/or susceptible to injection, but I'd rather stick with this for now (many reasons).
I just had a question about trying to use SELECT from WHILE, and figured that out and got it to echo the correct response on the page.
Now, how do I make it echo that as a value for an HTML text box? It won't work, and I've tried to look for typos but I don't know what I am doing, frankly.
I see that the $studentid and $teacherinfo show fine, I presume because they are normal variables.
Can I somehow define two more variables for first name and last name further up in the page so that I do not need to include so much code in each input (and to keep it from being buggy)?
Here is my code for the page. The inputs will be hidden, but I have been making them text boxes for debugging purposes.
<?php
$connection = mysql_connect($serverName, $userName, $password) or die('Unable to connect to Database host' . mysql_error());
$dbselect = mysql_select_db($dbname, $connection) or die("Unable to select database:$dbname" . mysql_error());
$studentid = $_POST['student_id'];
$teacherinfo = $_POST['teacher'];
$result = mysql_query("SELECT `first_name` FROM `students` WHERE student_id = '$studentid'",$connection);
?>
</head>
<body>
<div align="center">
<form method="post" action="vote_post.php">
<h1>Vote for Teacher of the Month</h1>
<h4>(step 2 of 2)</h4>
<h2>Confirm the Information Below</h2>
<h5>Student id: <?php echo $studentid ?></br>
Student first name: <?php
while($row = mysql_fetch_array($result)){
echo $row['first_name'];
}
?>
</br>
Voted for: <?php echo $teacherinfo ?>
</h5>
<input type="text" name="student_id" value="<?php echo $studentid; ?>"/></br>
<input type="text" name="first_name" value="<?php while($row = mysql_fetch_array($result)){
echo $row['first_name'];
} ?>"/>
</br>
<input type="text" name="last_name" value="<?php while($row = mysql_fetch_array($result)){
echo $row['last_name'];
} ?>"/>
</br>
<input type="text" name="teacher" value="<?php echo $teacherinfo; ?>"/></br>
<input type="submit" value="Submit Vote" class="inputbutton"/></br></br></br>
</form>
You can't use while because your query return only one student. You have to use if instead of while. If your query return many students you can use while.
Try this code:
<?php
if($row = mysql_fetch_array($result)){
?>
Student first name: <?php echo $row['first_name'];?>
</br>
Voted for: <?php echo $teacherinfo ?></h5>
<input type="text" name="student_id" value="<?php echo $studentid; ?>"/></br>
<input type="text" name="first_name" value="<?php echo $row['first_name'];?>"/></br>
<input type="text" name="last_name" value="<?php echo $row['last_name'];?>"/></br>
<input type="text" name="teacher" value="<?php echo $teacherinfo; ?>"/></br>
<input type="submit" value="Submit Vote" class="inputbutton"/></br></br></br>
<?php
}
?>
I hope this help.
I tried to build a code that will help you. Remember that you use last_name, but does not return the field in SQL.
</head>
<body>
<div align="center">
<form method="post" action="vote_post.php">
<h1>Vote for Teacher of the Month</h1>
<h4>(step 2 of 2)</h4>
<h2>Confirm the Information Below</h2>
<?php
$connection = mysql_connect($serverName, $userName, $password) or die('Unable to connect to Database host' . mysql_error());
$dbselect = mysql_select_db($dbname, $connection) or die("Unable to select database:$dbname" . mysql_error());
$studentid = $_POST['student_id'];
$teacherinfo = $_POST['teacher'];
$result = mysql_query("SELECT `first_name`,`last_name`,`student_id` FROM `students` WHERE student_id = $studentid",$connection);
while($row = mysql_fetch_array($result)){
echo "<h5>Student id: $row['student_id'] </br>" .
"Student first name: $row['first_name'] </br>" .
"Voted for: $teacherinfo </h5> " .
"<input type='text' name='student_id' value='$row[\'student_id\']' /></br>" .
"<input type='text' name='first_name' value='$row[\'first_name\']' /></br>" .
"<input type='text' name='last_name' value='$row[\'last_name\']' /></br>" .
"<input type='text' name='teacher' value='$teacherinfo' /></br>"
}
?>
<input type="submit" value="Submit Vote" class="inputbutton"/></br></br></br>
</form>
WHILE I left because I do not know if your query can return more than one record, despite appearing to be a key. If you do not need to check the response of the Ragnar.