How to use mysql_fetch_row in CodeIgniter - php

Can anyone tell me how to use mysql_fetch_row in CodeIgniter? My code is like this:
while($row=mysql_fetch_row($result)){
for($i=1;$i<=$spasi;$i++){
echo "| ";
}
if ($row[8] === 'Koleris') {
$keputusan = "<font color=green>$row[8]</font>";
} elseif ($row[8] === 'Melankolis') {
$keputusan = "<font color=blue>$row[8]</font>";
} elseif ($row[8] === 'Plegmatis') {
$keputusan = "<font color=purple>$row[8]</font>";
} elseif ($row[8] === 'Sanguins') {
$keputusan = "<font color=red>$row[8]</font>";
} elseif ($row[8] === '?') {
$keputusan = "<font color=black>$row[8]</font>";
} else {
$keputusan = "<b>$row[8]</b>";
}
echo "<font color=red>$row[1]</font> = $row[2] (Koleris = $row[4], Melankolis = $row[5], Plegmatis = $row[6], Sanguins = $row[7]) : <b>$keputusan</b><br>";
/*panggil dirinya sendiri*/
get_subfolder($row[0], $spasi + 1);
}
What will the code be if we will use this code in CodeIgniter?

try this :
$query = $this->db->query("YOUR QUERY");
$row = $query->row();
if (isset($row))
{
echo $row->title;
echo $row->name;
echo $row->body;
}

To save your old code with minimal changes, you can try such approach
while($row=$query->unbuffered_row()){
$row = array_values($row);

You can simply write this : -
$result contains multiple records from query
foreach($result as $row){
for($i=1;$i<=$spasi;$i++){
echo "| ";
}
if ($row[8] === 'Koleris') {
$keputusan = "<font color=green>$row[8]</font>";
} elseif ($row[8] === 'Melankolis') {
$keputusan = "<font color=blue>$row[8]</font>";
} elseif ($row[8] === 'Plegmatis') {
$keputusan = "<font color=purple>$row[8]</font>";
} elseif ($row[8] === 'Sanguins') {
$keputusan = "<font color=red>$row[8]</font>";
} elseif ($row[8] === '?') {
$keputusan = "<font color=black>$row[8]</font>";
} else {
$keputusan = "<b>$row[8]</b>";
}
echo "<font color=red>$row[1]</font> = $row[2] (Koleris = $row[4], Melankolis = $row[5], Plegmatis = $row[6], Sanguins = $row[7]) : <b>$keputusan</b><br>";
/*panggil dirinya sendiri*/
get_subfolder($row[0], $spasi + 1);
}

Related

How to display "no results found" on mysqli_fetch_assoc?

I am trying to display a message when the search finds 0 results. I have tried several different ways to do it but nothing works; I always get a blank page or manage to display the message even when search finds results.
The code:
$post = $_POST;
if (isset($post['Kohderyhmä']) &&
isset($post['Näytön_aste']) &&
isset($post['Vaikutusten_vahvuus']) &&
isset($post['Käyttökelpoisuus']))
{
$Kohderyhmä = $post['Kohderyhmä'];
$Näytön_aste = $post['Näytön_aste'];
$Vaikutusten_vahvuus = $post['Vaikutusten_vahvuus'];
$Käyttökelpoisuus = $post['Käyttökelpoisuus'];
}
else
{
echo '<!-- Virhe -->'; /*die ('<h2>Ei hakutermiä syötetty. Avaa haku</h2>');*/
}
$count = 0;
$and = "";
$query = "";
if (!empty($Kohderyhmä) && $Kohderyhmä !="Kaikki" ) {
if ($count > 0) {
$and = " AND ";
}
$count++;
$query = $query.$and."`Kohderyhmä` LIKE '%".$Kohderyhmä."%'";
}
if (!empty($Näytön_aste) && $Näytön_aste !="Kaikki" ) {
if ($count > 0) { $and = " AND "; }
$count++;
$query = $query.$and."`Näytön aste` LIKE '%".$Näytön_aste."%'";
}
if (!empty($Vaikutusten_vahvuus) && $Vaikutusten_vahvuus !="Kaikki" ) {
if ($count > 0) { $and = " AND "; }
$count++;
$query = $query.$and."`Vaikutusten vahvuus` LIKE '%".$Vaikutusten_vahvuus."%'";
}
if (!empty($Käyttökelpoisuus) && $Käyttökelpoisuus !="Kaikki" ) {
if ($count > 0) { $and = " AND "; }
$count++;
$query = $query.$and."`Käyttökelpoisuus` LIKE '%".$Käyttökelpoisuus ."%'";
}
if ($count > 0) {
$query = "SELECT * FROM `tietokanta` WHERE ".$query;
} else {
$query = "SELECT * FROM `tietokanta`";
}
//echo $query;
if ($results = $conn->query($query)) {
while ($row = $results->fetch_assoc()) {
echo '<h3>' . $row['Nimi'] . '</h3>';
echo $row['Kokonaisarvio'] ."<br /><br />";
echo $row['Kuvaus'] ."<br /><br />";
}
} else {
echo '<h2>Haku ei tuottanut yhtään tulosta. Muuta hakuehtoja ja hae uudestaan.</h2>';
}
I have tried to find tutorials and other tips from the internet and php.net pages but I can't find a working solution.
That is quite strange because this question is asked every week. Not to mention you can read on mysqli_query's manual page that this function's return value is always positive, no matter whether it was found anything or not
Change your code to this
if ($results = $conn->query($query)->fetch_all(MYSQLI_ASSOC)) {
foreach ($results as $row) {
echo '<h3>' . $row['Nimi'] . '</h3>';
echo $row['Kokonaisarvio'] ."<br /><br />";
echo $row['Kuvaus'] ."<br /><br />";
}
} else {
echo '<h2>Haku ei tuottanut yhtään tulosta. Muuta hakuehtoja ja hae uudestaan.</h2>';
}
Try this following code
$results = $conn->query($query);
if ($results->num_rows >= 1){
while ($row = $results->fetch_assoc()) {
echo '<h3>' . $row['Nimi'] . '</h3>';
echo $row['Kokonaisarvio'] ."<br /><br />";
echo $row['Kuvaus'] ."<br /><br />";
}
}
else{
echo '<h2>Haku ei tuottanut yhtään tulosta. Muuta hakuehtoja ja hae uudestaan.</h2>';
}

Why can't I echo all variables correctly?

Edited, please scroll down
I am trying to display 3 variables which consist of data stored in a SQL database. However, only the first gets echoed successfully (topLeftUrl). It is worth noting that the same PHP file also receives data from an input (also in the same PHP file) and stores it in the same SQL database. This code was written for testing purposes and may not be entirely safe.
//Connect
$con = mysqli_connect ("localhost","noneedtoknow","noneedtoknow","noneedtoknow");
if (mysqli_connect_errno())
{
echo "Error: ", mysql_connect_error(), "<br>";
die ();
}
//Store input in SQL database
$result = mysqli_query ($con, "SELECT * FROM edit");
$message = stripslashes ($_POST ['message']);
if ($message !== '') {
mysqli_query ($con, "UPDATE edit SET cont='$message' WHERE id='message'"); }
$topLeftNew = ($_POST ['topLeftUrl']);
if ($topLeftNew !== '') {
mysqli_query ($con, "UPDATE edit SET cont='$topLeftNew' WHERE id='topLeft'"); }
$topRightNew = ($_POST ['topRightUrl']);
if ($topRightNew !== '') {
mysqli_query ($con, "UPDATE edit SET cont='$topRightNew' WHERE id='topRight'"); }
//First echo
while ($row = mysqli_fetch_array ($result))
{
if ($row["id"] == "topLeft" && $done2 == 0)
{
$topLeftUrl = $row["cont"];
}
}
echo "<input type=\"text\" name=\"topLeftUrl\" value=\"" . $topLeftUrl . "\">";
//Second echo
while ($row = mysqli_fetch_array ($result))
{
if ($row["id"] == "topRight" && $done3 == 0)
{
$topRightUrl = $row["cont"];
}
}
echo "<input type=\"text\" name=\"topRightUrl\" value=\"" . $topRightUrl . "\">";
//Third echo
while ($row = mysqli_fetch_array ($result))
{
if ($row["id"] == "message" && $done == 0)
{
echo $row["cont"];
}
}
Edit:
I updated the code, and the problem seems to have changed. For some reason, echo $messageCont; displays an old value of cont WHERE id='message'. The database itself is updated successfully, though, and I see the new value of cont once I refresh the page/re-submit the form. Why do I not see the current value of cont immediately after form submission, though? Here is the new code:
/* Before <!DOCTYPE html> */
//Connect
$con = mysqli_connect ("localhost","noneedtoknow","noneedtoknow","noneedtoknow");
if (mysqli_connect_errno())
{
echo "Error: ", mysql_connect_error(), "<br>";
die ();
}
//Query and update
$result = mysqli_query ($con, "SELECT * FROM edit");
$message = stripslashes ($_POST ['message']);
if ($message !== '') {
mysqli_query ($con, "UPDATE edit SET cont='$message' WHERE id='message'"); }
$topLeftNew = ($_POST ['topLeftUrl']);
if ($topLeftNew !== '') {
mysqli_query ($con, "UPDATE edit SET cont='$topLeftNew' WHERE id='topLeft'"); }
$topRightNew = ($_POST ['topRightUrl']);
if ($topRightNew !== '') {
mysqli_query ($con, "UPDATE edit SET cont='$topRightNew' WHERE id='topRight'"); }
//Query again and read
$done0 = 0;
$done1 = 0;
$done2 = 0;
mysqli_data_seek ($result, 0);
while ($row = mysqli_fetch_array ($result))
{
if ($row["id"] == "topLeft" && $done0 == 0)
{
$topLeftUrl = $row["cont"];
$done0 = 1;
}
else if ($row["id"] == "topRight" && $done1 == 0)
{
$topRightUrl = $row["cont"];
$done1 = 1;
}
else if ($row["id"] == "message" && $done2 == 0)
{
$messageCont = $row["cont"];
$done2 = 1;
}
else null;
}
/* After <!DOCTYPE html> */
/* Form code was omitted as it works perfectly. It is in this same file, though. */
echo "<input type=\"text\" name=\"topLeftUrl\" value=\"" . $topLeftUrl . "\">";
echo "<input type=\"text\" name=\"topRightUrl\" value=\"" . $topRightUrl . "\">";
echo $messageCont;
Any help is appreciated.
Edit: I only had to replace mysqli_data_seek () with the line beginning by $result (cut/paste). Thank you.
I ran into this same problem on my site....you run multiple mysql_fetch_array() on the same query ($result)...I thought this would work on my site but this failed for all but the first of 6 while loops which all referenced the same query on my site (I'm sorry but I don't remember the exact error message in my error_log). Try condensing your 3 while loops into 1 loop, something like this:
while ($row = mysqli_fetch_array ($result)) {
if ($row["id"] == "topLeft" && $done2 == 0) {
$topLeftUrl = $row["cont"];
} else if ($row["id"] == "topRight" && $done3 == 0) {
$topRightUrl = $row["cont"];
} else if ($row["id"] == "message" && $done == 0) {
echo $row["cont"];
} else null;
}
echo "<input type=\"text\" name=\"topRightUrl\" value=\"" . $topRightUrl . "\">";
echo "<input type=\"text\" name=\"topLeftUrl\" value=\"" . $topLeftUrl . "\">";

MySQL Error: Duplicate 'Candidate Name'

I have created a MySQL database along with a front-end to manipulate it using PHP. However, while I can add content to the database manually, I cannot utilize my front-end. When I try to submit the data in my front-end's form fields, I receive the prompt "Duplicate Candidate Name."
The following PHP file is my general script for displaying the front-end:
<?php
if(isset($_POST['sbmtbtn']) && ($_POST['sbmtbtn'] != ""))
{
$desc = strip_tags($_POST['txtdesc']);
$date = glb_func_chkvl($_POST['txtdate']);
$first = glb_func_chkvl($_POST['txtfirst']);
$last = glb_func_chkvl($_POST['txtlast']);
$skill = glb_func_chkvl($_POST['txtskill']);
$sub1 = glb_func_chkvl($_POST['txtsub1']);
$sub2 = glb_func_chkvl($_POST['txtsub2']);
$person = glb_func_chkvl($_POST['txtperson']);
$company = glb_func_chkvl($_POST['txtcompany']);
$location = glb_func_chkvl($_POST['txtlocation']);
$complex = glb_func_chkvl($_POST['complex']);
$sts = glb_func_chkvl($_POST['lststs']);
$dt = date('Y-m-d');
$emp = $_SESSION['sesadmin'];
$sqryquestion_info
= "SELECT candi_first
FROM question_info
WHERE candi_first='$first'";
if(isset($_POST['frmtyp']) && ($_POST['frmtyp'] == "add"))
{
$srsquestion_info =mysql_query($sqryquestion_info);
$rows = mysql_num_rows($srsquestion_info);
if($rows > 0)
{
$gmsg = "<font color=red size=2>Duplicate Candidate Name . Record not saved</font>";
}
else
{
$iqryquestion_info="insert into question_info(
candi_first,candi_last,date,
skill,subtype_1,
subtype_2,person_int,
comp_name,loc_int,complex_lvl,
type_int,question_candi,q_crton,
q_crtby)
values('$first','$last','$date','$skill','$sub1','$sub2','$person','$company',
'$location','$complex','$sts','$desc','$dt','$emp')";
$irsquestion_info = mysql_query($iqryquestion_info);
if($irsquestion_info==true)
{
$gmsg = "<font color=green size=2>Record saved successfully</font>";
}
else
{
$gmsg = "<font color=red size=2>Record not saved</font>";
}
}
}
if(isset($_POST['frmtyp']) && ($_POST['frmtyp'] == "edit"))
{
$id = $_REQUEST['hdnedit'];
$pg = $_REQUEST['hdnpg'];
$countstart = $_REQUEST['hdncntstrt'];
$sqryquestion_info .=" and ques_id !=$id";
$srsquestion_info = mysql_query($sqryquestion_info);
$rows = mysql_num_rows($srsquestion_info);
if($rows > 0)
{
?>
<script>location.href="view_all_questions.php?sts=d&pg=<?php echo $pg;?>&countstart=<?php echo $countstart;?><?php echo $srchval;?>";</script>
<?php
}
else
{
$uqryquestion_info="update question_info set
date ='$date',
candi_first ='$first',
candi_last ='$last',
skill ='$skill',
subtype_1 ='$sub1',
subtype_2 ='$sub2',
person_int ='$person',
comp_name ='$company',
loc_int ='$location',
complex_lel ='$complex',
type_int ='$company',
question_candi ='$desc',
q_mdfdon ='$dt',
q_mdfdby ='$emp' ";
$uqryquestion_info .= " where ques_id=$id";
$ursquestion_info = mysql_query($uqryquestion_info);
if($ursquestion_info==true)
{
?>
<script>location.href="view_all_questions.php?sts=y&pg=<?php echo $pg;?>&countstart=<?php echo $countstart;?><?php echo $srchval;?>";
</script>
<?php
}
else
{
?>
<script>location.href="view_all_questions.php?sts=n&pg=<?php echo $pg;?>&countstart=<?php echo $countstart;?><?php echo $srchval;?>";
</script>
<?php
}
}
}
/*********************************** End Editing ******************************************************/
}
?>
Here begins my "main file" for editing:
<?php
if(isset($_POST['sbmtbtn']) && ($_POST['sbmtbtn'] != ""))
{
$desc = strip_tags($_POST['txtdesc']);
$date = glb_func_chkvl($_POST['txtdate']);
$first = glb_func_chkvl($_POST['txtfirst']);
$last = glb_func_chkvl($_POST['txtlast']);
$skill = glb_func_chkvl($_POST['txtskill']);
$sub1 = glb_func_chkvl($_POST['txtsub1']);
$sub2 = glb_func_chkvl($_POST['txtsub2']);
$person = glb_func_chkvl($_POST['txtperson']);
$company = glb_func_chkvl($_POST['txtcompany']);
$location = glb_func_chkvl($_POST['txtlocation']);
$complex = glb_func_chkvl($_POST['complex']);
$sts = glb_func_chkvl($_POST['lststs']);
$dt = date('Y-m-d');
$emp = $_SESSION['sesadmin'];
$sqryquestion_info="select candi_first
from question_info
where candi_first='$first'";
if(isset($_POST['frmtyp']) && ($_POST['frmtyp'] == "add"))
{
$srsquestion_info =mysql_query($sqryquestion_info);
$rows = mysql_num_rows($srsquestion_info);
if($rows > 0)
{
$gmsg = "<font color=red size=2>Duplicate Candidate Name . Record not saved</font>";
}
else
{
$iqryquestion_info="insert into question_info(
candi_first,candi_last,date,
skill,subtype_1,
subtype_2,person_int,
comp_name,loc_int,complex_lvl,
type_int,question_candi,q_crton,
q_crtby)
values('$first','$last','$date','$skill','$sub1','$sub2','$person','$company',
'$location','$complex','$sts','$desc','$dt','$emp')";
$irsquestion_info = mysql_query($iqryquestion_info);
if($irsquestion_info==true)
{
$gmsg = "<font color=green size=2>Record saved successfully</font>";
}
else
{
$gmsg = "<font color=red size=2>Record not saved</font>";
}
}
}
if(isset($_POST['frmtyp']) && ($_POST['frmtyp'] == "edit"))
{
$id = $_REQUEST['hdnedit'];
$pg = $_REQUEST['hdnpg'];
$countstart = $_REQUEST['hdncntstrt'];
$sqryquestion_info .=" and ques_id !=$id";
$srsquestion_info = mysql_query($sqryquestion_info);
$rows = mysql_num_rows($srsquestion_info);
if($rows > 0)
{
?>
<script>location.href="view_all_questions.php?sts=d&pg=<?php echo $pg;?>&countstart=<?php echo $countstart;?><?php echo $srchval;?>";</script>
<?php
}
else
{
$uqryquestion_info="update question_info set
date ='$date',
candi_first ='$first',
candi_last ='$last',
skill ='$skill',
subtype_1 ='$sub1',
subtype_2 ='$sub2',
person_int ='$person',
comp_name ='$company',
loc_int ='$location',
complex_lel ='$complex',
type_int ='$company',
question_candi ='$desc',
q_mdfdon ='$dt',
q_mdfdby ='$emp' ";
$uqryquestion_info .= " where ques_id=$id";
$ursquestion_info = mysql_query($uqryquestion_info);
if($ursquestion_info==true)
{
?>
<script>location.href="view_all_questions.php?sts=y&pg=<?php echo $pg;?>&countstart=<?php echo $countstart;?><?php echo $srchval;?>";
</script>
<?php
}
else
{
?>
<script>location.href="view_all_questions.php?sts=n&pg=<?php echo $pg;?>&countstart=<?php echo $countstart;?><?php echo $srchval;?>";
</script>
<?php
}
}
}
/*********************************** End Editing ******************************************************/
}
?>

Combine two columns in one table to one output

I a have a table like this:
and I want to combine colums 'uitvoeringid' and 'uitvoeringoms' and output as one with space between them.
This is my class:
public function getBanden($id = NULL, $merk = NULL, $seizoen = NULL)
{
$sql = "SELECT * FROM Uitvoering";
if(!empty($id))
{
$sql .= " WHERE uitvoeringid=:id";
if(!empty($merk)) { $sql .= " AND merkcode=:merk"; }
if(!empty($seizoen)) { $sql .= " AND uitvoeringseizoen=:seizoen"; }
}
else if(!empty($merk))
{
$sql .= " WHERE merkcode=:merk";
if(!empty($seizoen)) { $sql .= " AND uitvoeringseizoen=:seizoen"; }
$sql .= " ORDER BY uitvoeringvoertuigtype ASC, uitvoeringoms ASC";
}
try
{
$stmt = $this->db->prepare($sql);
if(!empty($id)) { $stmt->bindParam(":id", $id, PDO::PARAM_INT); }
if(!empty($merk)) { $stmt->bindParam(":merk", $merk, PDO::PARAM_STR); }
if(!empty($seizoen)) { $stmt->bindParam(":seizoen", $seizoen, PDO::PARAM_STR); }
$stmt->execute();
$this->bandenlijst = $stmt->fetchAll(PDO::FETCH_OBJ);
$stmt->closeCursor();
return $this->bandenlijst;
}
catch (Exception $e)
{
die ( $e->getMessage() );
}
}
This is a part of my file where I output the data:
if(isset($_POST['band_submit']) && $_POST['band_submit'] == "Zoek" || isset($_GET['merk']) && isset($_GET['type']) && isset($_GET['profiel']))
{
$merk = NULL;
$seizoentype = NULL;
if(isset($_POST['band_submit']) && $_POST['band_submit'] == "Zoek")
{
if($_POST['band_seizoen'] != "0") { $seizoentype = $_POST['band_seizoen']; }
$merk = $_POST['band_merk'];
}
else if(isset($_GET['merk']) && isset($_GET['type']))
{
if($_GET['type'] != "0") { $seizoentype = $_GET['type']; }
$merk = $_GET['merk'];
}
else { $seizoentype = NULL; $merk = NULL; }
$strSeizoen = NULL;
if ($seizoentype == "ZO") { $strSeizoen = "Onze zomerbanden"; }
elseif ($seizoentype == "WI") { $strSeizoen = "Onze winterbanden"; }
elseif ($seizoentype == "AS") { $strSeizoen = "Onze All-seasonbanden"; }
elseif ($seizoentype == "OV") { $strSeizoen = "Onze Overige banden"; }
else { $strSeizoen = "Alle A-merken en topklasse huismerken"; }
echo "\t\t\t\t\t<h2>" . $strSeizoen . "</h2>
\t\t\t\t\t<br />\n";
$merken = $merkclass->getMerken($merk);
$banden = $bandclass->getBanden(NULL, $merk, $seizoentype);
$nCount = 0;
$selband = NULL;
?>
<img src="http://www.website.net/logos/<?php echo str_replace(".png", "_150.png", $merken[0]->merk_logo); ?>" width="150" class="logo" alt="<?php echo $merken[0]->merk_naam; ?>"/>
<div id="merken">
<ul>
<?php
foreach($banden as $band)
{
?>
<li><a href="http://example-website.com/<?php
echo $band->merkcode;?>/<?php if(isset($seizoentype) && $seizoentype == "ZO") {echo "zomerbanden";}
else if ($seizoentype == "WI") {echo "winterbanden";}
else if ($seizoentype == "AS") {echo "all-season-banden";}
else if ($seizoentype == "OV") {echo "overig";}
else{ echo "alle-types";}?>/<?php echo $band->uitvoeringid;?>">
<?php echo str_replace(array(' ', ',', '/', '!'), '-',strtolower($band->uitvoeringoms));?>
</a>
</li>
<?php
if(isset($_GET['profiel']) && $band->uitvoeringid == $_GET['profiel']) { $selband = $band; }
$nCount++;
}
if(empty($selband) && count($banden) > 0)
{
$selband = $banden[0];
}
else if(count($banden) > 0)
{
}
else
{
echo "\t\t\t\t\t\t\t<li>Nothing Found</li>\n";
}
?>
</ul>
<div class="clearboth"></div>
</div>
How can I manage to keep the working of this the same but combine 'uitvoeringid' and 'uitvoeringoms' to one output.
So in this part:
<a href="http://example-website.com/<?php
echo $band->merkcode;?>/<?php if(isset($seizoentype) && $seizoentype == "ZO") {echo "zomerbanden";}
else if ($seizoentype == "WI") {echo "winterbanden";}
else if ($seizoentype == "AS") {echo "all-season-banden";}
else if ($seizoentype == "OV") {echo "overig";}
else{ echo "alle-types";}?>/<?php echo $band->uitvoeringid;?>">
<?php echo str_replace(array(' ', ',', '/', '!'), '-',strtolower($band->uitvoeringoms));?>
</a>
I want this line <?php echo $band->uitvoeringid;?> to be 'uitvoeringoms' and 'uitvoeringid' combined to something like "test-2341"
I tried something like:
$sql = "SELECT concat(uitvoeringid, uitvoeringoms) AS single FROM Uitvoering";
But I still want to SELECT everything and not only (uitvoeringid, uitvoeringoms)
I got a bit lost trying to get this working in a good way. Can somebody help me please? :)
It was very hard to explain this in a good way for me so I hope you guys understand it.
Thanks
Isn't this what you are looking for? A space in the middle?
$sql = "SELECT *,concat(uitvoeringid, ' ', uitvoeringoms) AS single FROM Uitvoering";
Or simply:
echo $uitvoeringsid.' '.$uitvoeringoms;
You can have both everything and combined data:
$sql = "SELECT *, concat(uitvoeringid, " ", uitvoeringoms) AS single FROM Uitvoering";
You can use same statement to get all columns but you need to specify the columns names in statement, like below:
$sql = "SELECT concat(uitvoeringid, ' ' ,uitvoeringoms) AS single, Col_1, Col_2... FROM Uitvoering";

Error in file uploads

I am trying to upload files in dynamically created folder. It is working properly in my localhost but on server it is showing me error.
The Error is:-
Warning: move_uploaded_file() [function.move-uploaded-file]: open_basedir restriction in effect. File(/tmp/php323kcy) is not within the allowed path(s): (/home/) in /home/..../public_html/www..com./.../controller/add-product-process.php on line 83
My Php code is Here
<?php
include 'connection.php';
if(isset($_POST['product_name']) && ($_POST['category'])&& ($_POST['sub-category']) && ($_POST['product_qty']) && ($_POST['price']) && ($_POST['description']) && ($_POST['weight']))
{
$pname = $_POST['product_name'];
$category = $_POST['category'];
$scategory = $_POST['sub-category'];
$qty = $_POST['product_qty'];
$price = $_POST['price'];
$desc = $_POST['description'];
$dp=$_POST['dp'];
$offer= $_POST['offer'];
$size=$_POST['size'];
$weight=$_POST['weight'];
if(isset($_POST['color']))
{
$color=$_POST['color'];
}
else
{
$color = "N/A";
}
$query3 = mysql_query("select category_id from category where category_name='$category'");
$row3 = mysql_fetch_array($query3);
$query4 = mysql_query("select sub_category_id from sub_category where sub_category_name='$scategory'");
$row4 = mysql_fetch_array($query4);
$query1 = mysql_query("select product_id from stock");
while ($row = mysql_fetch_row($query1)) {
$id = $row[0];
}
$str1 = substr($id, 2, 5);
if (($str1 >= 1) && ($str1 < 9)) {
$str1++;
echo $new_id = "RD0000" . $str1;
} else if (($str1 >= 9) && ($str1 < 99)) {
$str1++;
echo $new_id = "RD000" . $str1;
} else if (($str1 >= 99) && ($str1 < 999)) {
$str1++;
echo $new_id = "RD00" . $str1;
} else if (($str1 >= 999) && ($str1 < 9999)) {
$str1++;
echo $new_id = "RD0" . $str1;
} else if (($str1 >= 9999) && ($str1 < 99999)) {
$str1++;
echo $new_id = "RD" . $str1;
} else {
echo 'Error: Contact PSSP.';
}
$dirPath = "../products/$new_id";
$imgpath = "products/$new_id";
$result = mkdir($dirPath, 0755);
if ($result == 1) {
echo $dirPath . " has been created";
} else {
echo $dirPath . " has NOT been created";
}
define ("FILEREPOSITORY","../products/$new_id");
for ($i = 0; $i < sizeof($_FILES['uploadfile']['name']); $i++) {
echo $path=$new_id.$i;
$filename = $dirPath.$path.'.jpeg';
if (is_uploaded_file($_FILES['uploadfile']['tmp_name'][$i]))
{
$filename2 = $imgpath."/".$path.'.jpeg';
$fl[$i]=$filename2;
if ($_FILES['uploadfile']['type'][$i] != "image/jpeg")
{
echo "<p>Must be Image file.</p>";
}
else if(file_exists($filename))
{
echo "already exist";
}
else
{
//$name = $_POST['corname'];
$result = move_uploaded_file($_FILES['uploadfile']['tmp_name'][$i], FILEREPOSITORY."/$path.jpeg");
echo "result is".$result;
if ($result == 1)
{
echo "<p>File successfully uploaded.</p>";
}
else
{
echo "not uploaded";
}
}
}
}
$files=implode(',',$fl);
>
$query2 = mysql_query("insert into stock(product_id,product_name,category,sub_category,quantity,price,dp,offer,description,image,size,weight,color)values('$new_id','$pname','$row3[0]','$row4[0]','$qty','$price','$dp','$offer','$desc','$files','$size','$weight','$color')");
if (!$query2) {
echo mysql_error();
} else {
?>
<script language="javascript" type="text/javascript">
// Print a message
alert('Successfully Added..');
// Redirect to some page of the site.
window.location = '../add-product.php';
</script>
<?php
}
}
else
{
echo "Error in page...";
}
?>
Please aware me about the problem..
Thanks in advance
Your hosting account is configured in such a way that PHP uploads are not functional:
Apache stores temporary files in /tmp.
PHP is not allowed to read files outside /home/ (funnily enough, it's apparently allowed to read files from other users).
The first path is controlled with the upload_tmp_dir directive. The second path is controlled with the open_basedir directive. As far as I know, both of them are global settings you aren't allowed to change.
You need to contact support and ask for help to get this fixed.

Categories