User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 456,277 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,126 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 3020 | Replies: 3
Reply
Join Date: Sep 2007
Posts: 13
Reputation: bhakti.thakkar is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
bhakti.thakkar bhakti.thakkar is offline Offline
Newbie Poster

how to pass a php array to a sql query

  #1  
Nov 12th, 2007
hi all,
i have a string for example:
$str="The annual general meeting is scheduled on ::date:: at ::time::.
you all are requested to be present on ::date:: at ::time::."

i store it in the Templates_T table in my DB. Template_ID =1 [for eg.] now this format can be used further for drafting letters or creating newsletters etc..

Now in newsletters.php page for every ::date:: and ::time:: instance, for Template_ID 1 , i load textboxes in array for date[<input type =text name="dispdate[]">] and <select NAME="hours[]" > </select> <select NAME="minutes[]" > </select>in array[] for time. user enters some date and time in the textboxes and <select> respectively. now while updating the content in DB it should go as:
Step 1 : select the entire content from Template_T where Template_ID=1
Step 2: modify the message and replace ::date:: and ::time:: with relevant textbox values , <select > values. some thing like below
$EntireContent="The annual general meeting is scheduled on 'ValueOfBoxInArray' at 'ValueOfSelectInArray'.
you all are requested to be present on 'ValueOfBoxInArray' at 'ValueOfSelectInArray'."
$sql=mysql_query("insert into mytable_t values('$EntireContent')");

how will i accomplish this?
Thanks
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2006
Location: New Jersey
Posts: 1,422
Reputation: stymiee is on a distinguished road 
Rep Power: 5
Solved Threads: 34
Moderator
stymiee's Avatar
stymiee stymiee is offline Offline
He's No Good To Me Dead

Re: how to pass a php array to a sql query

  #2  
Nov 12th, 2007
MySQL doesn't recognize arrays much less PHP arrays. You need to put one piece of information in each field. If you want the entire contents of an array to go into one field either serialize it or convert it to a string.
John Conde
Brainyminds | Merchant Account Services | I Love Code
IT'S HERE: Merchant Accounts 101 Everything you need to know about merchant accounts!
Reply With Quote  
Join Date: Aug 2007
Location: Morrisdale, PA
Posts: 52
Reputation: JeniF is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 5
JeniF's Avatar
JeniF JeniF is offline Offline
Junior Poster in Training

Solution Re: how to pass a php array to a sql query

  #3  
Nov 12th, 2007
Your request sounds a bit confusing, but I did a quick project that accomplishes something that you may be able to at least work with.
Please note this took me about 15-20 mintes to complete and test..there is much more that could be done to this
(I also used a Simple Calendar Widget - Cross-Browser Javascript pop-up calendar by Anthony Garrett scw.js)

I have one page called newspage.php here is that page
** Note, I converted the date output from the default of 0000-00-00 to 00-00-0000**
connect to your DB

<?php require_once('Connections/phpForum.php'); ?>

<?php
mysql_select_db($database_phpForum, $phpForum);
$query_rsNews = "SELECT * FROM templates_t";
$rsNews = mysql_query($query_rsNews, $phpForum) or die(mysql_error());
$row_rsNews = mysql_fetch_assoc($rsNews);
$totalRows_rsNews = mysql_num_rows($rsNews);
[/inlinecode]
Form to hold values

<form id="form1" name="form1" method="post" action="">
<table width="554" border="0">
<tr>
<td width="54"><a href="modify.php?id=<? echo $row_rsNews['id'];?>" >[Modify]</a></td>
<td width="490">&nbsp;</td>
</tr>
<tr>
<td><input name="id" type="hidden" id="id" value="<?php echo $row_rsNews['id']; ?>" /></td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td><?php echo $row_rsNews['text']; ?> on
<? $e = strtotime($row_rsNews['date']); ?>

<?php echo strftime('%m-%d-%Y', $e); ?> at <?php echo $row_rsNews['time']; ?> <?php echo $row_rsNews['AMPM']; ?></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><?php echo $row_rsNews['text2']; ?> on <?php echo strftime('%m-%d-%Y', $e); ?> at&nbsp; <?php echo $row_rsNews['time']; ?> <?php echo $row_rsNews['AMPM']; ?></td>
</tr>
</table>
</form>


end html

mysql_free_result($rsNews);

Notice that I am carrying across via the URL the "id" of the Record to modify.
On the modify page I use the following code to display the current record and update it
[inlinecode]
<?php require_once('Connections/phpForum.php'); ?>
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
$updateSQL = sprintf("UPDATE templates_t SET `date`=%s, `time`=%s, AMPM=%s, text=%s, text2=%s WHERE id=%s",
GetSQLValueString($_POST['date'], "date"),
GetSQLValueString($_POST['time'], "date"),
GetSQLValueString($_POST['AMPM'], "text"),
GetSQLValueString($_POST['text'], "text"),
GetSQLValueString($_POST['text2'], "text"),
GetSQLValueString($_POST['id'], "int"));

mysql_select_db($database_phpForum, $phpForum);
$Result1 = mysql_query($updateSQL, $phpForum) or die(mysql_error());

$updateGoTo = "newspage.php";
if (isset($_SERVER['QUERY_STRING'])) {
$updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
$updateGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $updateGoTo));
}

$colname_rsModifyNews = "-1";
if (isset($_GET['id'])) {
$colname_rsModifyNews = (get_magic_quotes_gpc()) ? $_GET['id'] : addslashes($_GET['id']);
}
mysql_select_db($database_phpForum, $phpForum);
$query_rsModifyNews = sprintf("SELECT * FROM templates_t WHERE id = %s", $colname_rsModifyNews);
$rsModifyNews = mysql_query($query_rsModifyNews, $phpForum) or die(mysql_error());
$row_rsModifyNews = mysql_fetch_assoc($rsModifyNews);
$totalRows_rsModifyNews = mysql_num_rows($rsModifyNews);
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script type='text/JavaScript' src='scw.js'></script>

</head>

<body>
<form id="form1" name="form1" method="POST" action="<?php echo $editFormAction; ?>">
<table width="591" border="0">
<tr>
<td width="585"><input name="id" type="hidden" id="id" value="<?php echo $row_rsModifyNews['id']; ?>" /><?php echo $row_rsModifyNews['id']; ?></td>
</tr>
<tr>
<td><input name="text" type="text" id="text" value="<?php echo $row_rsModifyNews['text']; ?>" size="95" /></td>
</tr>
<tr>
<td>on
<input name="date" type="text" id="date" value="
<?php
if ($row_rsModifyNews['date']== "0000-00-00")
{
echo '';
}
else
if ($row_rsModifyNews['date'] != NULL )
{
echo $row_rsModifyNews['date'];
} ?>"
/>
<img src='JobPosting/images/scw.gif' alt='Click Here' width="16" height="16" title='Click Here' onClick="scwShow(document.getElementById('date'),this);" /> at
<input name="time" type="text" id="time" value="<?php echo $row_rsModifyNews['time']; ?>" />
&nbsp; &nbsp;&nbsp;
<select name="AMPM" id="AMPM">
<?php
do {
?>
<option value="<?php echo $row_rsModifyNews['AMPM']?>"><?php echo $row_rsModifyNews['AMPM']?></option>
<?php
} while ($row_rsModifyNews = mysql_fetch_assoc($rsModifyNews));
$rows = mysql_num_rows($rsModifyNews);
if($rows > 0) {
mysql_data_seek($rsModifyNews, 0);
$row_rsModifyNews = mysql_fetch_assoc($rsModifyNews);
}
?>
</select></td>
</tr>
<tr>
<td><p>
<input name="text2" type="text" id="text2" value="<?php echo $row_rsModifyNews['text2']; ?>" size="95" />
&nbsp; &nbsp;</p> </td>
</tr>
<tr>
<td><div align="center">
<input type="submit" name="Submit" value="Update" />
</div></td>
</tr>
</table>
<input type="hidden" name="MM_update" value="form1">
</form>
</body>
</html>
<?php
mysql_free_result($rsModifyNews);
?>
I can send you the javascript for the calendar if you wish..let me know
I keep hitting "escape", but I'm still here!!!!
:}
Reply With Quote  
Join Date: Sep 2007
Posts: 13
Reputation: bhakti.thakkar is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
bhakti.thakkar bhakti.thakkar is offline Offline
Newbie Poster

Re: how to pass a php array to a sql query

  #4  
Nov 13th, 2007
hi Jeni,
First of all THANKS for looking into the problem and trying out an example for me. now have a look to what i am actually doing...
i am passing the Template_ID in my href
FIRST OF ALL LOAD THE TEMPLATE CONTENT:
// Oracle 9i is my DB and I am using PEAR

if ($Template_ID !='') {
$sql="select TemplateName_VC, Header_Txt, Footer_Txt, Content_Txt from Templates_T where Template_ID='$Template_ID'";
$rs=$db->getRow($sql,DB_FETCHMODE_ASSOC);

$content=$rs['header_txt'].'<br>';
$content.=$rs['content_txt'].'<br>';
$content.=$rs['footer_txt'];

// find every instance of date $Content_TX=explode("::date::",$content);
}

<?
$Dstr = '::date::'; // DATE INSTANCE
$Tstr = '::time::'; // TIME INSTANCE
// find every instance of date and create textboxes
if(substr_count($content, $Dstr)>0) {
$date_string=implode('<INPUT TYPE="text" class=txtbox NAME="dispdate[]" style="width:60">', $Content_TX);
} else {
$date_string = $Content_TX;
}
// find every instance of time and create <select>
if(substr_count($content, $Tstr)>0) {
$time=explode("::time::",$date_string);
$time_string=implode('<SELECT NAME="hours[]" class=sel><option value="">HH</option>'.$droplist.'</SELECT>&nbsp;: <SELECT NAME="minutes[]" class=sel><option value="">MM</option>'.$ttdroplist.'</SELECT>', $time);
} else {

$time_string="";
}
?>
<script>
function SaveAndClose() {
frm=document.mainform;
frm.action="save_newsletter.php";
frm.submit();
}

</script>
<table class="table" width="80%" cellspacing=0 cellpadding=1 nof=ly border=1>
<INPUT TYPE="hidden" NAME="Template_ID" value="<?=$Template_ID?>">
<tr>
<td colspan="2"><INPUT TYPE="button" value="Preview " class="button" onclick="Previewwin();"></td>
</tr>
<tr id="toolbar">
<td id="styleformat"><BR>
<?=$time_string?>
<BR>&nbsp;
</td>
</tr>
<td colspan="2"><INPUT TYPE="button" value="SAVE" class="button" onclick="SaveAndClose();"></td>

</table>
////////////////// END OF newsletters.php ////////////////////////////////////////////


/////////////////////////////////////
save_newsletters.php
/////////////////////////////////////
Now here is the problem of coding .... i dont know how to code here .....

Can you help me here...????

BUT, again THANKS for the try you have done... i will surely try to get help from what you have coded...

---
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 6:51 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC