PHP/HTML get data from database using combobox - php

I have combobox that loads the value from database. I want when user select value from the combobox the form should load the corresponding values from database into html form. Here is code I am trying:
<!DOCTYPE html>
<html>
<head>
<title>Obracun plata</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link href="layout.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>
<?php
error_reporting(0);
mysql_connect("localhost", "root", "");
mysql_select_db("zaposleni");
$query = "SELECT * FROM radnici;";
$rezultat = mysql_query($query);
?>
<div id="wrapper">
<div id="logo">
<h1>Obračun plata</h1>
<p>Income calculator</p>
</div>
<div id="menu">
<ul>
<li>Pocetna</li>
<li>Podaci o radnicima </li>
<li>Spisak radnih mjesta</li>
<li>Obracun</li>
<li>Spisak gradova</li>
<li>Spisak mjeseci</li>
</ul>
</div>
<div id="page">
<div id="content">
<br/>
<td>Radnici:</td>
<td>
<select name="Radnik">
<?php
error_reporting(0);
mysql_connect("localhost", "root", "");
mysql_select_db("zaposleni");
$query = "SELECT * FROM radnici;";
$rezultat = mysql_query($query);
for($data = mysql_fetch_row($rezultat); $data != null; $data = mysql_fetch_row($rezultat))
echo "<option value='" . $data[0] . "'>" . $data[1] . " " . $data[2];
?>
</select>
<p> </p>
<?php
error_reporting(0);
if($_POST["Radnik"] == "")
echo "<div style='color:red'>Niste izabrali radnika</div>";
else
{
mysql_connect("localhost", "root", "");
mysql_select_db("zaposleni");
$query1 = "SELECT * FROM radnici WHERE id ='" . $_POST["Radnik"] . "';";
$query2 = "SELECT grad.naziv FROM grad, radnici WHERE radnici.grad_id=grad.id AND radnici.id='" . $_POST["Radnik"] . "';";
$query3 = "SELECT radno_mjesto.naziv FROM radno_mjesto, radnici WHERE radnici.radno_mjesto_id=radno_mjesto.id AND radnici.id='" . $_POST["Radnik"] . "';";
$rezultat1 = mysql_query($query1);
$rezultat2 = mysql_query($query2);
$rezultat3 = mysql_query($query3);
$data1 = mysql_fetch_row($rezultat1);
$data2 = mysql_fetch_row($rezultat2);
$data3 = mysql_fetch_row($rezultat3);
$prezime = $data1[1];
$ime = $data1[2];
$brtelefona = $data1[3];
$mail = $data1[4];
$brtekuceg = $data1[5];
$strucnasprema = $data1[6];
$godradnogstaza = $data1[7];
$adresa = $data1[8];
$grad = $data2[0];
$radnomjesto = $data3[0];
}
?>
<form action="podaci_o_radnicima.php" method="post">
<table>
<tr>
<td>Prezime:</td>
<td><input name="prezime" type="text" size="45" maxlength="45" value="<?php echo $prezime?>" readonly="readonly" /></td>
</tr>
<tr>
<td>Ime:</td>
<td><input type="text" name="ime" maxlength="45" size="45" value="<?php echo $ime ?>" readonly="readonly"/></td>
</tr>
<tr>
<td>Broj telefona:</td>
<td><input type="text" name="broj_telefona" maxlength="45" size="45" value=" <?php echo $brtelefona ?>" readonly="readonly"/></td>
</tr>
<tr>
<td>E-mail:</td>
<td><input type="text" name="email" maxlength="45" size="45" value="<?php echo $mail ?>" readonly="readonly"/></td>
</tr>
<tr>
<td>Broj tekuceg:</td>
<td><input type="text" name="broj_tekuceg" maxlength="45" size="45" value="<?php echo $brtekuceg ?>" readonly="readonly"/></td>
</tr>
<tr>
<td>Strucna sprema:</td>
<td><input type="text" name="strucna sprema" maxlength="45" size="45" value="<?php echo $strucnasprema ?>" readonly="readonly"/></td>
</tr>
<tr>
<td>Godina radnog staza:</td>
<td><input type="text" name="god_radnog_staza" maxlength="45" size="45" value="<?php echo $godradnogstaza ?>" readonly="readonly"/></td>
</tr>
<tr>
<td>Adresa:</td>
<td><input type="text" name="adresa" maxlength="45" size="45" value="<?php echo $adresa ?>" readonly="readonly"/></td>
</tr>
<tr>
<td>Grad:</td>
<td><input type="text" name="grad" maxlength="45" size="45" value="<?php echo $grad ?>" readonly="readonly"/></td>
</tr>
<tr>
<td>Radno mjesto:</td>
<td><input type="text" name="radno_mjesto" maxlength="45" size="45" value="<?php echo $radnomjesto ?>" readonly="readonly"/></td>
</tr>
</table>
<table>
<tr><td></td><td colspan="4" align="center"><input type="submit" value="Prikazi"> </td></tr>
</table>
</form>
</div>
</body>
</html>

Related

corresponds to your MariaDB server version for the right syntax to use near '3g,charging=999,diagnostic=299 ,screen=2190 ,water=299 ,camera' at line 1

I am getting the following error:
corresponds to your MariaDB server version for the right syntax to use near '3g,charging=999,diagnostic=299 ,screen=2190 ,water=299 ,camera' at line 1
i have checked all the possible issues that can cause this problem but none of them work for me.
The code is the html code is
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "apple";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sku = $_GET['sku'];
$model = $_GET['model'];
$charging = $_GET['charging'];
$diagnostic = $_GET['diagnostic'];
$screen = $_GET['screen'];
$water = $_GET['water'];
$camera = $_GET['camera'];
$earspeaker = $_GET['earspeaker'];
$frame = $_GET['frame'];
$headphone = $_GET['headphone'];
$homebtn = $_GET['homebtn'];
$mic = $_GET['mic'];
$network = $_GET['network'];
$other = $_GET['other'];
$sensor = $_GET['sensor'];
$software = $_GET['software'];
$speaker = $_GET['speaker'];
$volumebtn = $_GET['volumebtn'];
$filesdestination = $_GET['filesdestination'];
//echo $_GET['model'];
//$sql = "UPDATE table_3 SET lastname='Doe' WHERE id=2";
/*
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();*/
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="main.css">
<script src="main.js"></script>
<style>
table,
th,
td {
/*border: 1px solid black;*/
border-collapse: collapse;
text-align: center;
}
th,
td {
padding: 5px;
}
th {
text-align: center;
}
</style>
</head>
<body>
<center>
<form action="updatedata.php" method="post" enctype="multipart/form-data">
<table>
<tr>
<td>
<p for="sku">sku</p>
</td>
<td> <input type="text" id="sku" placeholder="sku" value="<?php echo $sku ?>" name="sku" required /></td>
</tr>
<tr>
<td>
<p for="the model">The model</p>
</td>
<td> <input type="text" id="model" value="<?php echo $model ?>" placeholder="phone model" name="model" required /></td>
</tr>
<tr>
<td>
<p for="charging">charging</p>
</td>
<td><input type="text" id="charging" value="<?php echo $charging ?>" placeholder="charging problem repair" name="charging"
required /></td>
</tr>
<tr>
<td>
<p for="diagnostic">Diagnostic</p>
</td>
<td> <input type="text" id="diagnostic" value="<?php echo $diagnostic ?>" placeholder="diagnostic" name="diagnostic" required /></td>
</tr>
<tr>
<td>
<p for="scren">Screen</p>
</td>
<td><input type="text" id="screen" value="<?php echo $screen ?>" placeholder="glass touch screen" name="screen" required /></td>
</tr>
<tr>
<td>
<p for="water">Water</p>
</td>
<td> <input type="text" id="water" value="<?php echo $water ?>" placeholder="water damage repair" name="water" required /></td>
</tr>
<tr>
<td>
<p for="water">Camera</p>
</td>
<td> <input type="text" id="camera" value="<?php echo $camera ?>" placeholder="camera repair" name="camera" required /></td>
</tr>
<tr>
<td>
<p for="earspeaker">ear speaker</p>
</td>
<td> <input type="text" id="earspeaker" value="<?php echo $earspeaker ?>" placeholder="ear speaker repair" name="earspeaker"
required /></td>
</tr>
<tr>
<td>
<p for="frame">Outer cover frame</p>
</td>
<td> <input type="text" id="frame" value="<?php echo $frame ?>" placeholder="frame and housing replacement" name="frame"
required /></td>
</tr>
<tr>
<td>
<p for="headphone">headphone jack</p>
</td>
<td> <input type="text" id="headphone" value="<?php echo $headphone ?>" placeholder="headphone jack repair" name="headphone"
required /></td>
</tr>
<tr>
<td>
<p for="homebtn">home button</p>
</td>
<td> <input type="text" id="homebtn" value="<?php echo $homebtn ?>" placeholder="home button repair" name="homebtn" required />
</td>
</tr>
<tr>
<td>
<p for="mic">Microphone</p>
</td>
<td> <input type="text" id="mic" value="<?php echo $mic ?>" placeholder="mic repair" name="mic" required /></td>
</tr>
<tr>
<td>
<p for="network">Network problem</p>
</td>
<td> <input type="text" id="network" value="<?php echo $network ?>" placeholder="network repair" name="network" required /></td>
</tr>
<tr>
<td>
<p for="other">other problems</p>
</td>
<td> <input type="text" id="other" value="<?php echo $other ?>" placeholder="other repair" name="other" required /></td>
</tr>
<tr>
<td>
<p for="sensor">sensors</p>
</td>
<td> <input type="text" id="sensor" value="<?php echo $sensor ?>" placeholder="sensor repair" name="sensor" required /></td>
</tr>
<tr>
<td>
<p for="software">Software flash</p>
</td>
<td> <input type="text" placeholder="software flash" value="<?php echo $software ?>" name="software" required /></td>
</tr>
<tr>
<td>
<p for="speaker">speaker</p>
</td>
<td> <input type="text" id="speaker" value="<?php echo $speaker ?>" placeholder="rear speaker repair" name="speaker" required />
</td>
</tr>
<tr>
<td>
<p for="volumebtn">volume button</p>
</td>
<td> <input type="text" id="volumebtn" value="<?php echo $volumebtn ?>" placeholder="volume button repair" name="volumebtn"
required /></td>
</tr>
<style>
.responsive {
max-width: 100%;
width: 10%;
max-height: 100%;
height: 5%;
}
</style>
<tr>
<?php
echo '
<img src="'.$filesdestination.'" alt="Nature" class="responsive">
';
?>
<td>
<p for="pic">Select the pic</p>
</td>
<td> <input type="file" id="pic" placeholder="phone model" value="update" name="pic" /></td>
</tr>
<tr>
<td></td>
<td><button type="submit" name="submit">upload</button></td>
</tr>
</table>
</form>
<h4>See what you add the data</h4>
</center>
</body>
</html>
The php code is
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "apple";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST['submit'])){
$sku = $_POST['sku'];
$charging = $_POST['charging'];
$diagnostic = $_POST['diagnostic'];
$screen = $_POST['screen'];
$water = $_POST['water'];
$camera = $_POST['camera'];
$earspeaker = $_POST['earspeaker'];
$frame = $_POST['frame'];
$headphone = $_POST['headphone'];
$homebtn = $_POST['homebtn'];
$mic = $_POST['mic'];
$network = $_POST['network'];
$other = $_POST['other'];
$sensor = $_POST['sensor'];
$software = $_POST['software'];
$speaker = $_POST['speaker'];
$volumebtn = $_POST['volumebtn'];
$model = $_POST['model'];
$file = $_FILES['pic'];
//print_r($file);
$fileName = $_FILES['pic']['name'];
$fileTmpName = $_FILES['pic']['tmp_name'];
$fileSize = $_FILES['pic']['size'];
$fileError = $_FILES['pic']['error'];
$fileType = $_FILES['pic']['type'];
$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));
$allowed = array('jpg', 'jpeg', 'png', 'pdf', 'svg');
if(in_array($fileActualExt, $allowed)){
if($fileError === 0){
if($fileSize < 1000000){
$fileNameNew = uniqid('', true).".".$fileActualExt;
$fileDestination = 'uploads/'.$fileNameNew;
move_uploaded_file($fileTmpName, $fileDestination);
//header("Location: appindex.php?uploadsuccess");
}else
echo "Your file is tooooooo big";
}else {
echo "There was an error to upload the file";
}
}else
{
echo "you cannot upload this file type";
}
}$sql = "UPDATE table_3 SET sku=$sku,model=$model,charging=$charging,diagnostic=$diagnostic,screen=$screen,
water=$water,camera=$camera,earspeaker=$earspeaker,frame=$frame,headphone=$headphone,homebtn=$homebtn,
mic=$mic,network=$network,other=$other,sensor=$sensor,software=$software,speaker=$speaker,volumebtn=$volumebtn,
filesdestination=$fileDestination WHERE model=$model";
if ($conn->query($sql) === TRUE) {
header("Location: index.html?uploadsuccess");
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
The error I am getting is:
Error: UPDATE table_3 SET sku=iph3,model=Iphone 3g,charging=999,diagnostic=299 ,screen=2190 ,water=299 ,camera=1499 ,earspeaker=1099 ,frame=1799 ,headphone=1200 ,homebtn=999 ,mic=1400 ,network=1600 ,other=1500 ,sensor=999 ,software=999 ,speaker=1400 ,volumebtn=999 ,filesdestination=uploads/5c80a5be538550.58960698.png WHERE model=Iphone 3g
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '3g,charging=999,diagnostic=299 ,screen=2190 ,water=299 ,camera' at line 1
Try this and print query:
$sql = "UPDATE table_3 SET sku='$sku',model='$model',charging='$charging',diagnostic='$diagnostic',screen='$screen',
water='$water',camera='$camera',earspeaker='$earspeaker',frame='$frame',headphone='$headphone',homebtn='$homebtn',
mic='$mic',network='$network',other='$other',sensor='$sensor',software='$software',speaker='$speaker',volumebtn='$volumebtn',
filesdestination='$fileDestination' WHERE model='$model'";
You are missing single quote for all values

PHP form submission error showing unsucess

I created this form with PHP and MySQL. Whenever I submit the form and click update button it is showing unsucess. I don't know what is going wrong and I need help finding the problem. I've attached the code below.
<?php
error_reporting(E_ALL & ~E_NOTICE);
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
$con = mysqli_connect("localhost","root","") or die("unable to connect");
mysqli_select_db($con,'studentdb');
if(isset($_GET['registernumber']))
$registernumber = html_entity_decode($_GET['registernumber']);
$query= "SELECT * FROM studenttble WHERE registernumber='$registernumber'";
$query_run = mysqli_query($con,$query);
while($row = mysqli_fetch_array($query_run, MYSQL_ASSOC))
{
$IID = $row['IID'];
$studentname = $row['studentname'];
$fathername = $row['fathername'];
$registernumber = $row['registernumber'];
$year = $row['$year'];
$dept = $row['$dept'];
$section = $row['section'];
$gender = $row['gender'];
$password = $row['password'];
$email = $row['email'];
$dob = $row['dob'];
$raddress = $row['raddress'];
$mnumber = $row['mnumber'];
}
?>
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post" id="form1" class="myform">
<table align="center">
<tr>
<td>
<input name="estudentname" type="text" id="studentname" class="wp-form-control wpcf7-text" value="<?php echo $studentname?>" />
</td>
</tr>
<br>
<tr>
<td>
<input name="efathername" type="text" id="fathername" class="wp-form-control wpcf7-text" value="<?php echo $fathername?>" />
</td>
</tr>
<tr>
<td>
<input type="hidden" name="registernumber" id="registernumber" class="wp-form-control wpcf7-text" value="<?php echo $registernumber?>" placeholder="Register Number" />
</td>
</tr>
<tr>
<td>
<input type="text" name="egender" id="gender" class="wp-form-control wpcf7-text" value="<?php echo $gender?>" />
</td>
</tr>
<tr>
<td><input type="text" name="eemail" type="email" id="email" class="wp-form-control wpcf7-text" value="<?php echo $email?>" />
</td>
</tr>
<tr>
<td>
<h3 class="table">Date Of Birth: <input type="text" name="edob" id="dob" type="date" class="dropdown-menu-left" value="<?php echo $dob?>" max="2017-02-08"/></td></tr>
</h3>
<tr>
<td> <input type="text" class="wp-form-control wpcf7-textarea" name="eraddress" cols="30" rows="10" value="<?php echo $raddress?>"></textarea>
</td>
</tr>
<tr>
<td> <input name="emnumber" type="text" id="mnumber" pattern="[789][0-9]{9}" class="wp-form-control wpcf7-text" max="10" value="<?php echo $mnumber?>" />
</td>
</tr>
<tr>
<td align="center">
<input type="submit" name="update" class="wpcf7-submit" value="update">
</td>
</tr>
</table>
</form>
<?php
if(isset($_POST['update']))
{
$con = mysqli_connect("localhost","root","") or die("unable to connect");
mysqli_select_db($con,'studentdb');
$estudentname = mysql_real_escape_string($_POST['estudentname']);
$efathername = mysql_real_escape_string($_POST['efathername']);
$egender = mysql_real_escape_string($_POST['egender']);
$eemail = mysql_real_escape_string($_POST['eemail']);
$edob = mysql_real_escape_string($_POST['edob']);
$eraddress = mysql_real_escape_string($_POST['eraddress']);
$emnumber = mysql_real_escape_string($_POST['emnumber']);
$targetid = mysqli_real_escape_string($_POST['registernumber']);
mysqli_select_db('studentdb');
$query = "UPDATE studenttble SET studentname ='$estudentname', fathername ='$efathername', gender ='$egender', email ='$eemail', dob ='$edob', raddress ='$eraddress', mnumber ='$emnumber' WHERE registernumber ='$targetid'";
$retval = mysqli_query( $query, $conn );
if($retval)
{
echo "sucess";
}
else
{
echo "unsucess";
}
}
?>

PHP/HTML return all rows from Oracle Database Query

I am trying to display multiple rows of data from a SQL query. I was able to build the reference so that table currently shows one row but was wondering how to show all rows using the same table structure.
<?php
include('connect.php');
oci_execute($sql);
while ($row = oci_fetch_array ($sql)) {
$pt = $row[0];
$eas = $row[1];
$shd = $row[2];
$epc = $row[3];
$tpc = $row[4];
$uid = $row[5];
}
?>
Using the table structure below
<table class="table" id="aut">
<tr>
<th>Pt</th>
<th>Eas</th>
<th>Cs</th>
<th>Or</th>
<th>Pr</th>
<th>Ct?</th>
<th>Al</th>
</tr>
<tr>
<td><input type="text" name="Pt" value="<?php echo $pt; ?>" class="form-control" readonly=""></td>
<td><input type="text" name="Eas" value="<?php echo $eas; ?>" class="form-control" readonly=""></td>
<td><input type="text" name="Cs" value="<?php echo $tpc; ?>" class="form-control" readonly=""></td>
<td><input type="text" name="Or" value="<?php echo $shd; ?>" class="form-control" readonly=""></td>
<td><input type="text" name="Pr" value="<?php echo $usd; ?>" class="form-control" readonly=""></td>
<td><input type="text" name="Al" value="<?php echo $epc; ?>" class="form-control" disabled></td>
</tr>
</table>
It'd be something like this:
<table class="table" id="aut">
<tr>
<th>Pt</th>
<th>Eas</th>
<th>Cs</th>
<th>Or</th>
<th>Pr</th>
<th>Ct?</th>
<th>Al</th>
</tr>
<?php
include('connect.php');
oci_execute($sql);
while ($row = oci_fetch_array ($sql)) {
$pt = $row[0];
$eas = $row[1];
$shd = $row[2];
$epc = $row[3];
$tpc = $row[4];
$uid = $row[5];
?>
<tr>
<td><input type="text" name="Pt" value="<?php echo $pt; ?>" class="form-control" readonly=""></td>
<td><input type="text" name="Eas" value="<?php echo $eas; ?>" class="form-control" readonly=""></td>
<td><input type="text" name="Cs" value="<?php echo $tpc; ?>" class="form-control" readonly=""></td>
<td><input type="text" name="Or" value="<?php echo $shd; ?>" class="form-control" readonly=""></td>
<td><input type="text" name="Pr" value="<?php echo $usd; ?>" class="form-control" readonly=""></td>
<td><input type="text" name="Al" value="<?php echo $epc; ?>" class="form-control" disabled></td>
</tr>
<?php
}
?>
</table>
Your code needs a lot of optimization, if you use DB calls and output in the same file, it's always wiser to put the output in a variable and only then echo the end result something like this:
<?php
include('connect.php');
oci_execute($sql);
$table = '<table class="table" id="aut">
<tr>
<th>Pt</th>
<th>Eas</th>
<th>Cs</th>
<th>Or</th>
<th>Pr</th>
<th>Ct?</th>
<th>Al</th>
</tr>';
while ($row = oci_fetch_array($sql)) {
$table .= '<tr>
<td><input type="text" name="Pt" value="' . $row[0] . '" class="form-control" readonly=""></td>
<td><input type="text" name="Eas" value="' . $row[1] . '" class="form-control" readonly=""></td>
<td><input type="text" name="Cs" value="' . $row[2] . '" class="form-control" readonly=""></td>
<td><input type="text" name="Or" value="' . $row[3] . '" class="form-control" readonly=""></td>
<td><input type="text" name="Pr" value="' . $row[4] . '" class="form-control" readonly=""></td>
<td><!-- What happened to Ct? --></td>
<td><input type="text" name="Al" value="' . $row[5] . '" class="form-control" disabled></td>
</tr>';
}
$table = '</table>';
echo $table;

Updating sql using php

Im trying to make an update form using php and updating the sql database but it seems not to work here. I tried updating the form and then it should say that its updated but it doesn't want to work. I need help please. Thanks
<?php
include 'connections/connection.php';
$updateinfo = #$_POST['update'];
$get_info = mysql_query("SELECT * FROM customer");
$id = #$_REQUEST['id'];
$get_row = mysql_fetch_assoc($get_info);
$db_cname = $get_row['customer_name'];
$db_cemail = $get_row['customer_email'];
$db_caddress = $get_row['customer_address'];
$db_cphone = $get_row['customer_phone'];
if ($updateinfo) {
$cname = strip_tags(#$_POST['cname']);
$cemail = strip_tags(#$_POST['cemail']);
$caddress = strip_tags(#$_POST['caddress']);
$cphone = strip_tags(#$_POST['cphone']);
//Submit the form to the database
$info_submit_query = mysql_query("UPDATE customer SET customer_name='$cname', customer_email='$cemail', customer_address='$caddress', customer_phone='$cphone' WHERE id='$id'") or die("Error! " . mysql_error());
echo "updated!";
}
else
{
echo "error " . mysql_error();
}
?>
<head>
<title>Customer Modification</title>
</head>
<body>
<table border="0" cellpadding="10" cellspacing="5" width="30%" style="margin-top:100px;" align="center" bgcolor="#FFB13C">
<form action="edit.php" method="post">
<tr>
<td><b>Customer Name:*</b></td>
<td><input type="text" size="50" name="cname" value="<?php echo $db_cname; ?>"></td>
</tr>
<tr>
<td><b>Customer Email:*</b></td>
<td><input type="text" size="50" name="cemail" value="<?php echo $db_cemail; ?>"></td>
</tr>
<tr>
<td><b>Customer Address:*</b></td>
<td><input type="text" size="50" name="caddress" value="<?php echo $db_caddress; ?>"></td>
</tr>
<tr>
<td><b>Customer Phone:*</b></td>
<td><input type="text" size="50" name="cphone" value="<?php echo $db_cphone; ?>"></td>
</tr>
<tr>
<td><input type="reset" name="reset" value="Reset"></td>
<td><input type="submit" name="update" value="Edit"></td>
</tr>
</form>
</table>
</body>

PHP, uploading 2 images, only uploads 1

I'm trying to upload 2 images at time. AVATAR and COVER IMG
When I select both avatar and cover and hit confirm, only avatar uploads
When I select only cover, cover uploads
Note that in mysql the name of the second image is displayed correctly. But missing in upload folder
My code:
<?php
define("_VALID_PHP", true);
require("../lib/config.ini.php");
$mysqli = new mysqli(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
$ID = $_GET['id'];
$name = $_POST['name'];
$category = $_POST['category'];
$sort = $_POST['sort'];
$tagline = $_POST['tagline'];
$exp = $_POST['exp'];
$company = $_POST['company'];
$country = $_POST['country'];
$links = $_POST['links'];
$created = $_POST['created'];
$add1 = $_POST['add1'];
$add2 = $_POST['add2'];
$add3 = $_POST['add3'];
$add4 = $_POST['add4'];
$add3 = $_POST['add5'];
$add4 = $_POST['add6'];
$notes = $_POST['notes'];
$uploaddir = '/data/web/creattium.com/sub/dir/admin/upload/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
$thegfi = $_FILES['userfile']['name'];
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Upload failed";
}
$uploadfile2 = $uploaddir . basename($_FILES['userfile2']['name']);
$thegfi2 = $_FILES['userfile2']['name'];
if (move_uploaded_file($_FILES['userfile2']['tmp_name'], $uploadfile2)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Upload failed";
}
mysqli_query($mysqli, "UPDATE yees SET name='$name',category='$category',sort='$sort',tagline='$tagline',exp='$exp',company='$company',country='$country',links='$links',created='$created',add1='$add1',add2='$add2',add3='$add3',add4='$add4',add5='$add5',add6='$add6',notes='$notes' WHERE id='$ID'");
if(!empty($_FILES['userfile']['name'])){
mysqli_query($mysqli, "UPDATE yees SET avatar='$thegfi' WHERE id='$ID'");
}else if(!empty($_FILES['userfile2']['name'])){
mysqli_query($mysqli, "UPDATE yees SET cover='$thegfi2' WHERE id='$ID'");
}
header("location: editemploy.php?id=".$ID."");
ADDED html
<form method="post" action="edit_yee.php?id=<?php echo $id;?>" enctype="multipart/form-data">
<?php
$a = mysql_query("SELECT * FROM yees WHERE id='$id'");
while($r = mysql_fetch_object($a)){
?>
<table cellpadding="0" cellspacing="0" class="forms">
<thead>
<tr>
<th colspan="2" class="left">Editing New Employee </th>
</tr>
</thead>
<tfoot>
<tr>
<td><input type="submit" class="button" value="Edit Employee"></td>
<td>Cancel</td>
</tr>
</tfoot>
<tbody>
<tr>
<th>Name:</th>
<td><input type="text" name="name" class="inputbox" size="55" value="<?php echo $r->name;?>"></td>
</tr>
<tr>
<th>Avatar:</th>
<td><input type="file" name="userfile" class="inputbox" style="width:337px;"></td>
</tr>
<tr>
<th>Cover:</th>
<td><input type="file" name="userfile2" class="inputbox" style="width:337px;"></td>
</tr>
<tr>
<th>Category:</th>
<td><input type="text" name="category"class="inputbox" size="55" value="<?php echo $r->category;?>"></td>
</tr>
<tr>
<th>Sort:</th>
<td><input type="text" name="sort"class="inputbox" size="55" value="<?php echo $r->sort;?>"></td>
</tr>
<tr>
<th>Tagline:</th>
<td><input type="text" name="tagline"class="inputbox" size="55" value="<?php echo $r->tagline;?>"></td>
</tr>
<tr>
<th>Exp:</th>
<td><input type="text" name="exp"class="inputbox" size="55" value="<?php echo $r->company;?>"></td>
</tr>
<tr>
<th>Company:</th>
<td><input type="text" name="company"class="inputbox" size="55" value="<?php echo $r->exp;?>"></td>
</tr>
<tr>
<th>Country:</th>
<td><input type="text" name="country"class="inputbox" size="55" value="<?php echo $r->country;?>"></td>
</tr>
<tr>
<th>Links:</th>
<td><input type="text" name="links"class="inputbox" size="55" value="<?php echo $r->links;?>"></td>
</tr>
<tr>
<th>Created:</th>
<td><input type="text" name="created"class="inputbox" size="55" value="<?php echo $r->created;?>"></td>
</tr>
<tr>
<th>Add1:</th>
<td><input type="text" name="add1"class="inputbox" size="55" value="<?php echo $r->add1;?>"></td>
</tr>
<tr>
<th>Add2:</th>
<td><input type="text" name="add2"class="inputbox" size="55" value="<?php echo $r->add2;?>"></td>
</tr>
<tr>
<th>Add3:</th>
<td><input type="text" name="add3"class="inputbox" size="55" value="<?php echo $r->add3;?>"></td>
</tr>
<tr>
<th>Add4:</th>
<td><input type="text" name="add4"class="inputbox" size="55" value="<?php echo $r->add4;?>"></td>
</tr>
<tr>
<th>Add5:</th>
<td><input type="text" name="add5"class="inputbox" size="55" value="<?php echo $r->add5;?>"></td>
</tr>
<tr>
<th>Add6:</th>
<td><input type="text" name="add6"class="inputbox" size="55" value="<?php echo $r->add6;?>"></td>
</tr>
<tr>
<th>Notes:</th>
<td><input type="text" name="notes"class="inputbox" size="55" value="<?php echo $r->notes;?>"></td>
</tr>
</tbody>
</table><br />
<?php }?>
</form>
<input type="file" name="userfile[]" class="inputbox" style="width:337px;">
Instead Of
<input type="file" name="userfile[]" class="inputbox" style="width:337px;" multiple>
Rename your inputs as:
<input type="file" name="userfile[]" class="inputbox" style="width:337px;">
Then, in php use:
$_FILES['userfile']['name'][0]
$_FILES['userfile']['name'][1]
...
More info here: http://www.php.net/manual/en/features.file-upload.multiple.php
Also check this:
Since PHP 5.2.12, the max_file_uploads configuration setting acts as a
limit on the number of files that can be uploaded in one request. You
will need to ensure that your form does not try to upload more files
in one request than this limit.

Categories