/    Sign up×
Community /Pin to ProfileBookmark

Writing to the Database = easy, editing from Database = Hard?!

I was able to learn enough to find out how to write to a mysql database using a basic html form. However, once the information has been written to the database, what is the next step to preview and update the information written in the database? I want to edit the information in the future. Can someone just write a little write up so I can get an idea and follow the syntax. Thanks.

p.s. View my code to see what I am doing.

[B]index.html[/B]

[code=php]<? $uploadNeed = 4; ?>
<table>
<form name=”form1″ enctype=”multipart/form-data” method=”post” action=”processfiles.php”>

<?

for($x=0;$x<$uploadNeed;$x++){
?>
<tr>
<td>Picture <? echo $x+1; ?> </td>
<td>
<input name=”uploadFile<? echo $x;?>” type=”file” id=”uploadFile<? echo $x;?>”> </td> </tr>

<?
// end of for loop
}
?>

<tr>

<td colspan=”3″ valign=”top” width=”100%”> <textarea cols=”80″ name=”desc”></textarea></td>
</tr>

<tr>
<td colspan=”2″>

<input name=”uploadNeed” type=”hidden” value=”<? echo $uploadNeed;?>”>
<input type=”submit” name=”Submit” value=”Submit”></td></tr>

</form>
</table>
[/code]

[B]processfiles.php[/B]

[code=php]<?
include(“db.php”);

$uploadNeed = $_POST[‘uploadNeed’];
$desc=$_POST[‘desc’];

$files_list=array();
// start for loop
for($x=0;$x<$uploadNeed;$x++){
$file_name = $_FILES[‘uploadFile’. $x][‘name’];
// strip file_name of slashes
$file_name = stripslashes($file_name);
$file_name = str_replace(“‘”,””,$file_name);
$copy = copy($_FILES[‘uploadFile’. $x][‘tmp_name’],”uploads/”. $file_name);
// check if successfully copied
if($copy){
$files=array_push($files_list,$file_name);
}
else{
echo “$file_name | could not be uploaded!<br>”;
}
} // end of loop

$sqlinsert=”Insert into pics (Intro,Pics1,Pics2,Pics3,Pics4) VALUES (‘$desc’,’$files_list[0]’,’$files_list[1]’,’$files_list[2]’,’$files_list[3]’)”;
mysql_query($sqlinsert,$db);

echo mysql_error();

echo “Success!”;
?>
[/code]

to post a comment
PHP

21 Comments(s)

Copy linkTweet thisAlerts:
@NightShift58Jan 27.2007 — You cannot use arrays this way:[code=php]$sqlinsert="Insert into pics (Intro,Pics1,Pics2,Pics3,Pics4) VALUES ('$desc','$files_list[0]','$files_list[1]','$files_list[2]','$files_list[3]')"; [/code]Instead, one option:[code=php]$sqlinsert="Insert into pics (Intro,Pics1,Pics2,Pics3,Pics4) VALUES ('$desc','{$files_list[0]}','{$files_list[1]}','{$files_list[2]}','{$files_list[3]}')"; [/code]Another option:[code=php]$sqlinsert="Insert into pics (Intro,Pics1,Pics2,Pics3,Pics4) VALUES ('$desc','" . $files_list[0] ."','" . $files_list[1] ."','" . $files_list[2] . "','" . $files_list[3] ."')"; [/code]
Copy linkTweet thisAlerts:
@pcthugJan 27.2007 — Could you post up your database design?
Copy linkTweet thisAlerts:
@gc40authorJan 27.2007 — 

[CODE]-- MySQL Administrator dump 1.4
--



-- ------------------------------------------------------
-- Server version 4.1.22-community-nt


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;

/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;

/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;

/*!40101 SET NAMES utf8 */;

/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;

/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;

/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO,ANSI_QUOTES,MYSQL323' */;


--

-- Create schema uploads
--



CREATE DATABASE IF NOT EXISTS uploads;

USE uploads;

--

-- Definition of table "pics"
--



DROP TABLE IF EXISTS "pics";

CREATE TABLE "pics" (

"ID" int(10) unsigned NOT NULL auto_increment,

"Intro" text NOT NULL,

"Pic1" varchar(100) NOT NULL default '',

"Pic2" varchar(100) NOT NULL default '',

"Pic3" varchar(100) NOT NULL default '',

"Pic4" varchar(100) NOT NULL default '',

PRIMARY KEY ("ID")

) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--

-- Dumping data for table "pics"
--



/*!40000 ALTER TABLE "pics" DISABLE KEYS */;

INSERT INTO "pics" ("ID","Intro","Pic1","Pic2","Pic3","Pic4") VALUES

(1,'','barbados.jpg','barbados.jpg','careers.php','LiveHelp.gif'),

(2,'hfghmnngmh','button-002.png','antilles_crossing.jpg','earth.png','LiveHelp.gif'),

(3,'hfghmnngmh','button-002.png','antilles_crossing.jpg','earth.png','LiveHelp.gif'),

(4,'hfghmnngmh','button-002.png','antilles_crossing.jpg','earth.png','LiveHelp.gif'),

(5,'','','','','');

/*!40000 ALTER TABLE "pics" ENABLE KEYS */;




/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;

/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;

/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;

/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;

/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;

[/CODE]
Copy linkTweet thisAlerts:
@pcthugJan 27.2007 — [code=php]
// edit sql
$sql = "UPDATE pics SET Intro = 'New Description' WHERE ID = 1";
[/code]
Copy linkTweet thisAlerts:
@NightShift58Jan 27.2007 — As per your PM, here's the basic select you requested:[code=php]<?
$DBserver = "localhost";
$DBuser = "username";
$DBpassw = "userpass";
$DBname = "uploads";

$DBconn = mysql_pconnect($DBserver,$DBuser,$DBpassw);
if (!$DBconn) :
echo("No connection to SQL Server");
exit;
endif;

$DBselect = mysql_select_db($DBname, $DBconn);
if (!$DBselect) :
echo("No connection to SQL Database");
exit;
endif;

$sql = "SELECT * FROM pics ";
$qry = mysql_query($sql) or die ("SQL Error: $sql<br>" . mysql_error());

WHILE ($row = mysql_fetch_array($qry)) :
echo "Description: " . $row['Intro'] . "<br />";
echo "Picture1: " . $row['Pic1'] . "<br />";
echo "Picture2: " . $row['Pic2'] . "<br />";
echo "Picture3: " . $row['Pic3'] . "<br />";
echo "Picture4: " . $row['Pic4'] . "<hr />";
ENDWHILE;
?>[/code]
Copy linkTweet thisAlerts:
@NightShift58Jan 27.2007 — [code=php]<?
$DBserver = "localhost";
$DBuser = "username";
$DBpassw = "userpass";
$DBname = "uploads";

$DBconn = mysql_pconnect($DBserver,$DBuser,$DBpassw);
if (!$DBconn) :
echo("No connection to SQL Server");
exit;
endif;

$DBselect = mysql_select_db($DBname, $DBconn);
if (!$DBselect) :
echo("No connection to SQL Database");
exit;
endif;

$sql = "SELECT * FROM pics ";
$qry = mysql_query($sql) or die ("SQL Error: $sql<br>" . mysql_error());

WHILE ($row = mysql_fetch_array($qry)) :
echo "Description: " . $row['Intro'] . "<br />";
echo "Picture1: " . $row['Pic1'] . "<br />";
echo "Picture2: " . $row['Pic2'] . "<br />";
echo "Picture3: " . $row['Pic3'] . "<br />";
echo "Picture4: " . $row['Pic4'] . "<hr />";
ENDWHILE;

?>[/code]
Pls load again....
Copy linkTweet thisAlerts:
@NightShift58Jan 27.2007 — Try this:[code=php]WHILE ($row = mysql_fetch_array($qry)) :
echo "Description: " . $row['Intro'] . "<br />";
echo "Picture1: <img src='/uploads/" . $row['Pic1'] . "'><br />";
echo "Picture2: <img src='/uploads/" . $row['Pic2'] . "'><br />";
echo "Picture3: <img src='/uploads/" . $row['Pic3'] . "'><br />";
echo "Picture4: <img src='/uploads/" . $row['Pic4'] . "'><br />";
ENDWHILE;[/code]
Copy linkTweet thisAlerts:
@NightShift58Jan 27.2007 — You gave me the wrong directory... Try this:[code=php]WHILE ($row = mysql_fetch_array($qry)) :
echo "Description: " . $row['Intro'] . "<br />";
echo "Picture1: <img src='/feature/uploads/" . $row['Pic1'] . "'><br />";
echo "Picture2: <img src='/feature//uploads/" . $row['Pic2'] . "'><br />";
echo "Picture3: <img src='/feature//uploads/" . $row['Pic3'] . "'><br />";
echo "Picture4: <img src='/feature//uploads/" . $row['Pic4'] . "'><br />";
ENDWHILE;[/code]
Copy linkTweet thisAlerts:
@gc40authorJan 27.2007 — I was a step ahead of you night ?

[code=php]WHILE ($row = mysql_fetch_array($qry)) :
echo "Description: " . $row['Intro'] . "<br />";
echo "Picture1: " . "<img src='/feature/uploads/" . $row['Pics1'] . "'><br /><br />";
echo "Picture2: " . "<img src='/feature/uploads/" . $row['Pics2'] . "'><br /><br />";
echo "Picture3: " . "<img src='/feature/uploads/" . $row['Pics3'] . "'><br /><br />";
echo "Picture4: " . "<img src='/feature/uploads/" . $row['Pics4'] . "'><br /><br />";
ENDWHILE;
[/code]
Copy linkTweet thisAlerts:
@gc40authorJan 27.2007 — That part is done, so we have them displaying. The next step is to be able to edit/update the information output...What do i do now?
Copy linkTweet thisAlerts:
@NightShift58Jan 27.2007 — PcThug poted a sample command to update... Use that...
Copy linkTweet thisAlerts:
@gc40authorJan 27.2007 — Ok, here is what we got so far:

[code=php]<?
include("db.php");

$uploadNeed = $_POST['uploadNeed'];
$desc=$_POST['desc'];

$files_list=array();
// start for loop
for($x=0;$x<$uploadNeed;$x++){
$file_name = $_FILES['uploadFile'. $x]['name'];
// strip file_name of slashes
$file_name = stripslashes($file_name);
$file_name = str_replace("'","",$file_name);
$copy = copy($_FILES['uploadFile'. $x]['tmp_name'],"uploads/". $file_name);
// check if successfully copied
if($copy){
$files=array_push($files_list,$file_name);
}
else{
echo "$file_name | could not be uploaded!<br>";
}
} // end of loop


$sqlinsert = "UPDATE pics SET Intro = '$desc', Pics1 = '{$files_list[0]}', Pics2 = '{$files_list[1]}', Pics3 = '{$files_list[2]}', Pics4 = '{$files_list[3]}' WHERE ID = 1";
mysql_query($sqlinsert,$db);


echo mysql_error();

echo "Success!";
?>
[/code]


Two problems exist:

#1. If I do not browse and select an image for say Pics4, then I get an error saying | could not be uploaded!. If the user decides he wants to update Pics 1-3, but not 4, then how would he do this?

#2. Why is it only displaying: | could not be uploaded! When the $file_name variable has been defined?
Copy linkTweet thisAlerts:
@gc40authorJan 27.2007 — help?
Copy linkTweet thisAlerts:
@NightShift58Jan 27.2007 — Untested:[code=php]<?
include("db.php");

$uploadNeed = $_POST['uploadNeed'];
$desc = $_POST['desc'];

$files_list = array();
// start for loop
for ($x=0;$x<$uploadNeed;$x++) {
IF (is_upload_file($_FILES['uploadFile'. $x]['name']) :
$file_name = $_FILES['uploadFile'. $x]['name'];
// strip file_name of slashes
$file_name = stripslashes($file_name);
$file_name = str_replace("'","",$file_name);
$copy = copy($_FILES['uploadFile'. $x]['tmp_name'],"uploads/". $file_name);
// check if successfully copied
if($copy){
$files = array_push($files_list,$file_name);
} else {
echo "$file_name | could not be uploaded!<br>";
}
ELSE :
$files = array_push($files_list,"");
ENDIF;
} // end of loop

$sqlinsert = "UPDATE pics SET Intro = '$desc', Pics1 = '{$files_list[0]}', Pics2 = '{$files_list[1]}', Pics3 = '{$files_list[2]}', Pics4 = '{$files_list[3]}' WHERE ID = 1";
mysql_query($sqlinsert,$db);

echo mysql_error();

echo "Success!";
?>[/code]
Copy linkTweet thisAlerts:
@gc40authorJan 28.2007 — Parse error: syntax error, unexpected ':' in processfiles.php on line 10
Copy linkTweet thisAlerts:
@gc40authorJan 28.2007 — ? Help?
Copy linkTweet thisAlerts:
@NightShift58Jan 28.2007 — Change Line 10 to:[code=php]IF (is_upload_file($_FILES['uploadFile'. $x]['name'])) : [/code]
Copy linkTweet thisAlerts:
@gc40authorJan 28.2007 — Another error:


Fatal error: Call to undefined function: is_upload_file() in processfiles.php on line 10
Copy linkTweet thisAlerts:
@NightShift58Jan 28.2007 — Change[:[code=php]IF (is_upload_file($_FILES['uploadFile'. $x]['name'])) :
[/code]
to:[code=php]IF (is_uploaded_file($_FILES['uploadFile'. $x]['name'])) : [/code]
Copy linkTweet thisAlerts:
@gc40authorJan 29.2007 — Same error still....

[code=php]<?
include("db.php");

$uploadNeed = $_POST['uploadNeed'];
$desc = $_POST['desc'];

$files_list = array();
// start for loop
for ($x=0;$x<$uploadNeed;$x++) {
IF (is_uploaded_file($_FILES['uploadFile'. $x]['name'])) : $file_name = $_FILES['uploadFile'. $x]['name'];
// strip file_name of slashes
$file_name = stripslashes($file_name);
$file_name = str_replace("'","",$file_name);
$copy = copy($_FILES['uploadFile'. $x]['tmp_name'],"uploads/". $file_name);
// check if successfully copied
if($copy){
$files = array_push($files_list,$file_name);
} else {
echo "$file_name | could not be uploaded!<br>";
}
ELSE :
$files = array_push($files_list,"");
ENDIF;
} // end of loop

$sqlinsert = "UPDATE pics SET Intro = '$desc', Pics1 = '{$files_list[0]}', Pics2 = '{$files_list[1]}', Pics3 = '{$files_list[2]}', Pics4 = '{$files_list[3]}' WHERE ID = 1";
mysql_query($sqlinsert,$db);

echo mysql_error();

echo "Success!";
?> [/code]
Copy linkTweet thisAlerts:
@gc40authorJan 31.2007 — bump.....
×

Success!

Help @gc40 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...