...

View Full Version : Confusing Myself



Webmonkey
08-07-2008, 12:29 AM
Hello,

I seem to be having a bit of trouble getting this code to work:

foreach($_POST as $n => $h) {
$h = mysql_real_escape_string($h);
$n = '$h';
echo $n.$h.'<br />';
}

What I want is for example:
$_POST['name'] = 'Dave';
I want $n to be name, and $h to be Dave, so I can use $name to insert Dave into the db.

Thanks if you can help

Steven

oesxyl
08-07-2008, 12:35 AM
must be:

foreach($_POST as $n => $h) {
$h = mysql_real_escape_string($h);
$n = $h;
// same thing in a single line:
$n = mysql_real_escape_string($h);
// because $n = $h, this will be: 'DaveDave<br />'
// to be 'nameDave<br/>' you must remove $n = $h
echo $n.$h.'<br />';
}

in this construct:


$n = '$h';

both $h and $n are variable, so is ok to write as $n = $h. Also the variable included between ' are not expanded.

regards

Webmonkey
08-07-2008, 12:37 AM
thanks for your post.

I did try this earlier, and for some reason it didn;t work. WOuld that script set $name = 'dave' each time so I can use it on a MySQL statement after the foreach?



<?php
// This is the main user 'dashboard'
if($_COOKIE['loggedin'] != 1 && !isset($_COOKIE['userName'])) {
header('location:index.php?do=login');
}
foreach($_POST as $n => $h) {
if($h == '' || $h == "") {
$error[] = "&bull; {$n} has no data in.";
}
}
if(sizeof($error) > 0) {
foreach($error as $msg)
echo $msg;
include("newproject.php");
die();
} else {
foreach($_POST as $n => $h) {
$h = mysql_real_escape_string($h);
$n = '$h';
//echo $n.$h.'<br />';
}
//die();
}
$query = mysql_query("INSERT INTO `projects` (`id`,`name`, `started`, `deadline`, `status`, `type`, `notes`, `cname`, `cemail`, `cphone`, `cspreq`) VALUES ('', '$name', '$started', '$deadline', '', '$type', '$notes', '$cname', '$cemail', '$cphone', '$spreq')") or die(mysql_error());
if($query == FALSE) {
echo "There was an error with {$query}, If this problem persists, Please contact the server administrator.";
} else {
header('location:index.php?do=showprojects');
}
?>

The script above running how it is now inserts blank rows in the db. However earlier, I managed to get the variables to be set, so it must be something to do with that and inserting them into the query.

Thankyou

oesxyl
08-07-2008, 12:51 AM
thanks for your post.
you are welcome, :)



<?php
// This is the main user 'dashboard'
if($_COOKIE['loggedin'] != 1 && !isset($_COOKIE['userName'])) {
header('location:index.php?do=login');
}
// add this line to see what have in $_POST
print_r($_POST);
foreach($_POST as $n => $h) {
// if($h == '' || $h == "") {
if($h == ''){ // same thing as above
$error[] = "&bull; {$n} has no data in.";
}
}
if(sizeof($error) > 0) {
foreach($error as $msg)
echo $msg;
include("newproject.php");
die();
} else {
foreach($_POST as $n => $h) {
$h = mysql_real_escape_string($h);
// $n = '$h';
//echo $n.$h.'<br />';
echo $n.': '.$h.'<br/>';
}
//die();
}
// you must check all this value if exists and have the values you expect
$query = mysql_query("INSERT INTO `projects` (`id`,`name`, `started`, `deadline`, `status`, `type`, `notes`, `cname`, `cemail`, `cphone`, `cspreq`) VALUES ('', '".$name."', '".$started."', '".$deadline."', '', '".$type."', '".$notes."', '".$cname."', '".$cemail."', '".$cphone."', '".$spreq."')") or die(mysql_error());
if($query == FALSE) {
echo "There was an error with {$query}, If this problem persists, Please contact the server administrator.";
} else {
header('location:index.php?do=showprojects');
}
?>

read the comments and look to the query, is modifiedregards

Webmonkey
08-07-2008, 01:05 AM
Thanks for your reply, I copy and pasted the code, however it still didn't work. Here is the MySQK table:



--
-- Table structure for table `projects`
--

CREATE TABLE `projects` (
`id` tinyint(3) NOT NULL auto_increment,
`name` text NOT NULL,
`started` int(15) NOT NULL,
`deadline` int(15) NOT NULL,
`status` tinyint(1) NOT NULL,
`type` varchar(225) NOT NULL,
`notes` longtext NOT NULL,
`cname` text NOT NULL,
`cemail` varchar(225) NOT NULL,
`cphone` varchar(11) NOT NULL,
`cspreq` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

--
-- Dumping data for table `projects`
--

INSERT INTO `projects` (`id`, `name`, `started`, `deadline`, `status`, `type`, `notes`, `cname`, `cemail`, `cphone`, `cspreq`) VALUES
(1, 'Tadley Computers', 111111, 111111, 0, 'CMS', 'Nm', 'Tadley PC', 'email@tadleypc.com', '29323890', ''),
(2, '', 0, 0, 0, '', '', '', '', '', ''),
(3, '', 0, 0, 0, '', '', '', '', '', '');


Thanks

oesxyl
08-07-2008, 01:19 AM
Thanks for your reply, I copy and pasted the code, however it still didn't work.
what is the output of the line with print_r ?

regards

Webmonkey
08-07-2008, 09:54 AM
with S for example in the th efields, print_r the query seems to retrurn :
[php]Array ( [name] => S [started] => s [deadline] => S [type] => S [notes] => S [cname] => S [cemail] => S [cphone] => S [spreq] => S [submit] => Go ) S: S[/]php]

Webmonkey
08-07-2008, 10:51 AM
I have fixed this error now.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum