This question already has answers here:
Move_uploaded_file() function is not working
(18 answers)
What does enctype='multipart/form-data' mean?
(9 answers)
Closed 3 months ago.
I'm making a function to insert some items to my database table but the images have to be saved locally to once uploaded. When I try doing that it keeps getting the error that it's not possible to upload it. Why's that?
<?php
if (isset($_GET['action']) and $_GET['action'] == 'add') { ?>
<div>
<form method='post' action='upload.php?action=add'>
<h2>Add new songs</h2>
<input type="hidden" name="action" value="insert">
<input type='text' name='title' class='textInput' placeholder='song title'>
<input type='text' name='composer' class='textInput' placeholder='composer'>
<input type='text' name='genre' class='textInput' placeholder='genre'>
<input type='text' name='instrument1' class='textInput' placeholder='instrument1'>
<input type='text' name='instrument2' class='textInput' placeholder='instrument2'>
<input type='text' name='instrument3' class='textInput' placeholder='instrument3'>
<input type='text' name='instrument4' class='textInput' placeholder='instrument4'>
<input type='text' name='instrument5' class='textInput' placeholder='instrument5'>
<div>
<label for="pngSheet">Image PNG file</label>
<input type="file" name='pngSheet' accept='.png'>
</div>
<div>
<label for="xmlSheet">Music XML file</label>
<input type="file" name='xmlSheet' accept='.musicxml'>
</div>
<input type='submit' name='submit' value='Add'>
</form>
</div>
<?php }
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if ($_POST['action'] == 'insert') {
$getTitle = $_POST['title'];
$getComposer = $_POST['composer'];
$getGenre = $_POST['genre'];
$getImgSheet = $_POST['pngSheet'];
$getSheet = $_POST['xmlSheet'];
$location = '../img/' . $getImgSheet;
if (move_uploaded_file($_POST['pngSheet'], $location)) {
$query = "INSERT INTO `imslp_sheets`(`sheets_title`, `sheets_composer`, `sheets_genre`, `sheets_img`,`sheets_xml`) VALUES ('$getTitle', '$getComposer', '$getGenre', '$getImgSheet','$getSheet')";
$result = $conn->query($query);
} else {
echo "Error uploading file $getImgSheet";
}
}
}
?>
Related
<?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
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
I have website that insert some info's like name , date , msg and IMG into mysqli database
here is my CODE
HTML & PHP CODE
<form action='' method='POST'>
<input type='date' name='bdate' min='2003-12-31' max='2016-04-02'><br><br>
<input type='text' name='send_name'/>
<input type='text' name='min_name' value='Some Name' disabled='true' ><br>
<textarea name='my_text' rows='11' cols='40'></textarea>
<input type='submit' name='submit' value='Send'>
<input type='file' name='Image' id='Image' >
</form>
if (isset($_POST['submit'])) {
$imgData = $_FILES['Image']['tmp_name'];
$imageProperties = $_FILES['Image']['tmp_name'];
$sql = "INSERT INTO output_images(imageType ,imageData)
VALUES('".$imageProperties['mime']."', '".$imgData."')";
$current_id = mysqli_query($connection,$sql) or die("<b>Error:</b> Problem on Image Insert<br/>" . mysqli_error());
if(isset($current_id)) {
echo "Image Upload Ok";
}
$imgData = $_FILES['Image']['tmp_name'];
$imageProperties = $_FILES['Image']['tmp_name'];
$min_name = "Ministry Of Intinor";
$dat = $_POST['bdate'];
$info = $_POST['my_text'];
$mySQL = mysqli_query($connection," INSERT INTO `mddb`.`ministry_tbl` (`sender_name`, `min_name`, `b_date`, `infos`,`img`,`img_name`) VALUES ('".$_SESSION['username']."', '".$min_name."', '".$dat."', '".$info."','".$imgData."','".$imageProperties['mime']."') ");
if ($mySQL) {
echo "Done";
}
}
My problem is the database not store the BLOB* img and its always [BLOB - 0 B]
NOTE
you have to use enctype="multipart/form-data" add input type="file" before submit button
<input type='date' name='bdate' min='2003-12-31' max='2016-04-02'><br><br>
<input type='text' name='send_name'/>
<input type='text' name='min_name' value='Some Name' disabled='true' ><br>
<textarea name='my_text' rows='11' cols='40'>
<input type='file' name='Image' id='Image' >
<input type='submit' name='submit' value='Send'>
</form>
A better approach is to not save images in database as it is not a good practice. Just save the uploaded image in a directory and save the path to that image in database. It would save you some database space and search time.
For reference please view my answer here PHP-SQL: Uploaded image displaying as junk text
my code is,
$query3 = "SELECT * FROM db_exam_skip WHERE user='$session'";
$result3 = mysql_query($query3) or die(mysql_error());
$length3 = mysql_num_rows($result3);
while($rows3 = mysql_fetch_array($result3))
{
$query1 = "SELECT * FROM db_exam_questions WHERE id='$rows3[ques_id]'";
$result1 = mysql_query($query1) or die(mysql_error());
$length1 = mysql_num_rows($result1);
}
if(isset($_POST['next']))
{
if(isset($_SESSION['list']))
{
mysql_data_seek($result1,$_SESSION['list']);
}
else
{
$list = $_POST['list'];
mysql_data_seek($result1,$list);
}
}
<?php
while($rows1 = mysql_fetch_row($result1))
{
$start = $rows1[0];
$_SESSION['start'] = $start;
?>
<form action="" method="post">
<p style="font-size:20px;font-weight:bold"><?php echo $rows1[5]; ?></p>
<ul style="list-style-type:none">
<input type='hidden' name='number' value='<?php echo $_SESSION['order']++; ?>' />
<input type='hidden' name='list' value='<?php echo $_SESSION['list']++; ?>' />
<input type='hidden' name='ques_id' value='<?php echo $rows1[0]; ?>' />
<input type='hidden' name='correct' value='<?php echo $rows1[10]; ?>' />
<li><input type="radio" name="answer" value="1" /> <?php echo $rows1[6]; ?> <br><br>
<input type="radio" name="answer" value="2" /> <?php echo $rows1[7]; ?> <br><br>
<input type="radio" name="answer" value="3" /> <?php echo $rows1[8]; ?> <br><br>
<input type="radio" name="answer" value="4" /> <?php echo $rows1[9]; ?> <br></li>
</ul>
<input type="submit" class="button4" value="Next" name="next" />
</form>
<?php
break;
}
?>
my question is, after the first question is loaded, when i press next button to load the second question I am getting the below error.
Warning: mysql_data_seek(): Offset 2 is invalid for MySQL result index 8 (or the query data is unbuffered) in C:\wamp\www\Albert\ICAMS\start_skip_question.php on line 15
I have tried a lot to solve this error. But till now no success. Is there any method to solve. Any help will be appreciated.
Thank you.
I guess the result set is empty.I think query is returning empty set.
You will get this error if result set is empty
check the PHP DOCS
First check if you are getting ant rows from the result
if (mysqli_num_rows($sql) > 0)
{
}
Below is the source code of a program. Can anyone help me to figure out the working of a program.
<?php
session_start();
?>
<?php
$aCaptcha = array (
array(),
array('crocodile'),
array('panda', 'panda bear', 'giant panda'),
array('pig'),
array('tiger'),
array('zebra'),
array('cow'),
array('elephant')
);
if (isset($_POST['register'])) {
$error = array();
if (!in_array(strtolower($_POST['captcha']), $aCaptcha[$_SESSION['captcha']])) {
$error['captcha'] = "<span style='color:red'>The name of the animal is not correct.</span>";
}
if (count($error) == 0) {
echo "<span style='color:red'>Thank you for completing the form.
We shall contact you soon.</span>";
die();
}
}
?>
<form action="index.php" method="post">
<?php
$_SESSION['captcha'] = rand(1, 7);
?>
<td colspan="3"><strong>Contact Form</strong></td>
<p>Full Name : <input type="text" name="Nmaes" value='' />
<p>Mobile No. : <input type="text" name="Nmaes" value='' />
<p>Email id : <input type="text" name="Nmaes" value='' />
<p>Subject : <input type="text" name="Nmaes" value='' />
<p>Message : <input type="text" name="Nmaes" value='' />
<p><img src="<?php echo $path;?>captcha/<?php echo $_SESSION['captcha'];?>.jpg" /></p>
<p>Type the name of the animal you see in the picture above. <input type="text" name="captcha" value='' />
<?php echo(isset($error['captcha']))?$error['captcha']:"";?></p>
<p><label> </label><input type='submit' name='register' value='register' /></p>
</form>
On the first page
random number between 1 and 7 is generated and stored in session
form is displayed
picture in the captcha directory is displayed based on the random number
On the second page
array with acceptable answers is generated - the keys are numbers 1 and 7 and the values are arrays of acceptable answers
the below code checks that the answer given by the user $_POST['captcha'] is one of the acceptable answers $aCaptcha[$_SESSION['captcha']]
if (!in_array(strtolower($_POST['captcha']), $aCaptcha[$_SESSION['captcha']])) {
$error['captcha'] = "<span style='color:red'>The name of the animal is not correct.</span>";
if acceptable then a message is printed out and PHP stops executing
echo "<span style='color:red'>Thank you for completing the form. We shall contact you soon.</span>";
die();