7 Replies - 240 Views - Last Post: 02 February 2013 - 03:01 PM

#1 wama_mms  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 54
  • Joined: 06-May 12

error Array to string conversion while inserting array

Posted 02 February 2013 - 02:49 PM

i am trying to insert an array into database when i use checkbox the id for each row that has a checkbox refers to it need to insert it in the database how can i do it ?? when i write array('1','2') it inserted well
<?php
include('db.php');

@$id = $_POST['id'];
if(isset($_POST['delete']))
{
	if(empty($id) || $id == 0)
	{
			echo '<script type="text/javascript">alert("اختر صف")</script>';
	}
	else
	{
		$array_data = array($id);

		echo print_r($array_data);
		
		for($i = 0; $i < count($array_data); $i++)
		{
		  mysql_query("INSERT INTO names (`mail`) VALUES ('$array_data[$i]')");
		}
		if(isset($delete))
		{
			echo '<script type="text/javascript">alert("تم المسح")</script>';
		}
	}
}
?>

This post has been edited by Dormilich: 02 February 2013 - 02:51 PM
Reason for edit:: please use [CODE] [/CODE] tags when posting code


Is This A Good Question/Topic? 0
  • +

Replies To: error Array to string conversion while inserting array

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,236
  • Joined: 08-June 10

Re: error Array to string conversion while inserting array

Posted 02 February 2013 - 02:53 PM

well, it depends how you set up your checkboxes.
Was This Post Helpful? 0
  • +
  • -

#3 wama_mms  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 54
  • Joined: 06-May 12

Re: error Array to string conversion while inserting array

Posted 02 February 2013 - 02:54 PM

View PostDormilich, on 02 February 2013 - 02:53 PM, said:

well, it depends how you set up your checkboxes.

there is the code of each checkbox
 <?php echo '<td><input type="checkbox" name="id[]" value='.$row['id'].'></td>' ?> 

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3480
  • View blog
  • Posts: 11,833
  • Joined: 12-December 12

Re: error Array to string conversion while inserting array

Posted 02 February 2013 - 02:55 PM

I don't know about the rest of the code, but you need curly brackets when quoting an array element:

mysql_query("INSERT INTO names (`mail`) VALUES ('{$array_data[$i]}')");


The mysql library is deprecated - use mysqli or PDO instead.
Use prepared statements.
Was This Post Helpful? 0
  • +
  • -

#5 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,236
  • Joined: 08-June 10

Re: error Array to string conversion while inserting array

Posted 02 February 2013 - 02:57 PM

$id is already an array and putting that into an array (line #13) you donít loop over your array data, but the original array (which returns the string "Array" when dumped into a string).
Was This Post Helpful? 0
  • +
  • -

#6 wama_mms  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 54
  • Joined: 06-May 12

Re: error Array to string conversion while inserting array

Posted 02 February 2013 - 02:58 PM

View Postandrewsw, on 02 February 2013 - 02:55 PM, said:

I don't know about the rest of the code, but you need curly brackets when quoting an array element:

mysql_query("INSERT INTO names (`mail`) VALUES ('{$array_data[$i]}')");


The mysql library is deprecated - use mysqli or PDO instead.
Use prepared statements.

same problem and the inserted value in the is Array no the actual value
Was This Post Helpful? 0
  • +
  • -

#7 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,236
  • Joined: 08-June 10

Re: error Array to string conversion while inserting array

Posted 02 February 2013 - 03:00 PM

View Postwama_mms, on 02 February 2013 - 10:58 PM, said:

same problem and the inserted value in the is Array no the actual value

see explanation above.
Was This Post Helpful? 0
  • +
  • -

#8 wama_mms  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 54
  • Joined: 06-May 12

Re: error Array to string conversion while inserting array

Posted 02 February 2013 - 03:01 PM

View PostDormilich, on 02 February 2013 - 02:57 PM, said:

$id is already an array and putting that into an array (line #13) you donít loop over your array data, but the original array (which returns the string "Array" when dumped into a string).

thanks :) i solved it by using the $id instead of putting into array
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1