Jump to content

post an array to table??


argan328

Recommended Posts

Hi

 

Need help posting an array to my mysql table (using mysql version 4.0).

 

I have an array:

$param = array(
	'calleridname' => $_POST['calleridname'],
	'callerid' => $_POST['callerid'],
	'phonenumber' => $_POST['phonenumber'],
	'message' => $_POST['message'],
);

and I have a database table with 4 fields

calleridname

callerid

phonenumber

message

 

and would like to post the array to those fields in the database. 

 

do I use

   $sql = "INSERT INTO messages
   			SET    calleridname='$calleridname',
    	                         create_date=CURDATE()
			 callerid='$callerid',
			 phonenumber='$phonenumber',
			 message='$message' ";

????  This doesn't seem to work for me.

 

Do I use a foreach loop?

Link to comment
Share on other sites

Can you post your full code (or at least the entire section related to mysql), and also tell us what happens when you try to store the data?  Is there any error message?  Is anything stored (even if it isn't what you expected to be stored)?

Link to comment
Share on other sites

it seems sometimes I get a mysql error but then other times when I mess around with the code I get no error nor exception thrown, it just doesn't work.

 

Here is the form on my main page which calls Phone.php:

 

				<form target="_blank" action="Phone.php" method="post" ENCTYPE="multipart/form-data">
			    <table style="height: 100%" width="100%" cellspacing="0" class="messageentry" border="1" cellpadding="4" bordercolor="#dcdcdc" rules="none">

    	  			  <tr>
			   		    <td colspan="2" align="center">
			   			<h2>Leave your message</h2>
			   			</td>
			 		  </tr>
                      <tr>
                            <td>Your Name:</td>
                            <td><input type="edit" size="25" name="calleridname"/></td>
                          </tr>

                          <tr>
                            <td>Your Phone Number:</td>
                            <td><input type="edit" size="14" name="callerid"/></td>
                          </tr>

                          <tr>
                            <td >Phonenumber:</td>
                            <td><input type="edit" size="14" name="phonenumber"/></td>
                          </tr>


                          <tr>
                            <td>Your Message:</td>
                            <td><textarea id="text" rows="5" cols="24"  name="message"/></textarea></td>
                          </tr>

					  <tr>
					    <td align="center" colspan="2" style="border:1px solid #000000">
				  		<br>
						  <input type="submit" name="submit" value="Submit" id="mySubmit" disabled="disabled" />
						</td>
					  </tr>
				</table>
				</form>

 

 

((You may wonder why I need an array to begin with and the answer is I need it for the next step in the site I'm creating))

and here is Phone.php:

<?php

$param = array(
	'calleridname' => $_POST['calleridname'],
	'callerid' => $_POST['callerid'],
	'phonenumber' => $_POST['phonenumber'],
	'message' => $_POST['message'],

);


//Connect To Database
$hostname="xxxx";
$username="xx_xx";
$password="xxxx";
$dbname="xx_xx";
$usertable="xxxx";
$yourfield = "xxxx";

$dbcnx = @mysql_connect($hostname, $username, $password);
if (!$dbcnx) {
echo '<p>Unable to connect to the ' .
     'database server at this time.</p>';
exit();
}

  // Select the database
if (!@mysql_select_db($dbname)) {
   exit('<p>Unable to locate the ' .
       'database at this time.</p>');
}


//implode the string-- I put this in because I thought I may need to use this feature for the array
$stringout = implode(",", $param);
  echo $stringout;

   // Submit message to the database-- I think this is where my problem is....

if (isset($_POST['$stringout'])) {
   $stringout = $_POST['$stringout'];
   $sql = "INSERT INTO messages
   			SET    calleridname='$calleridname',
    	                         create_date=CURDATE()
			 callerid='$callerid',
			 phonenumber='$phonenumber',
			 message='$message' ";

   if (@mysql_query($sql)) {
     echo '<p>Your array has been added.</p>';
   } else {
     echo '<p>Error adding submitted message: ' .
         mysql_error() . '</p>';
   }
}
?>

Link to comment
Share on other sites

thanks all for your input, without the comma after create_date=CURDATE() I get mysql error

Error adding submitted message: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'callerid='', phonenumber='', message=''' at line 4

 

when I put the comma in I get this error

Error adding submitted message: Duplicate entry '' for key 1

 

Do you know what this error means?

Link to comment
Share on other sites

Ok Barand I'm making progress... I finally have received the message "Your array has been added" and I jumped for joy, then I went to the database screen to see my results and the only field that was populated was the create_date... all the others were blank. 

 

Is there something in my code that would cause this?

 

Thanks again in advance!

Argan

Link to comment
Share on other sites

the thing is I have to have the $param array b/c of where I'm going next with the page (the vendor I'll be sending the info to will be looking for the $param array) - is it possible to have the $param array defined at the top and then down below when I'm inputting into the table spell it out like you've just indicated or will that screw things up?

Link to comment
Share on other sites

OK

 

If you keep the array

$sql = "INSERT INTO messages
   			SET    calleridname='{$param['calleridname']}',
    	                         create_date=CURDATE(),
			 callerid='{$param['calleridid']}',
			 phonenumber='{$param['phonenumber']}',
			 message='{$param['message']}' ";

Link to comment
Share on other sites

now I'm getting no response, neither of my echo's are showing up... I think my $stringout in the if statement may be messing things up.  if you recall from above I thought I was going to have to implode the array so I created $stringout and plopped it in the if statement.  I know the problem is somewhere in the code below... do you know why I would not get either of my echo statements? sorry to be such a pain on this one but I'm coming up on my deadline!  :-\

 

//implode the string
$stringout = implode(",", $param);
  echo $stringout;

   // Submit message to the database

  if (isset($_POST['$stringout'])) {
   $stringout = $_POST['$stringout'];
   $sql = "INSERT INTO messages
   			SET    calleridname='{$param['calleridname']}',
    	                         create_date=CURDATE(),
			 callerid='{$param['calleridid']}',
			 phonenumber='{$param['phonenumber']}',
			 message='{$param['message']}' ";

   if (@mysql_query($sql)) {
     echo '<p>Your array has been added.</p>';  //neither of these are showing up now??
   } else {
     echo '<p>Error adding submitted message: ' .
         mysql_error() . '</p>';
   }
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.