Jump to content

how can i see or edit a stored array[] in mysql?


unasemana

Recommended Posts

hi, i have the next files to store an array[] into a mysql database. Now i would like to create a file to see the stored data, as an info file....like a table with the results or something.

 

 

in phpmyadmin database  i see the stored data as: 

a:2:{i:0;s:10:"sentra b15";i:1;s:10:"sentra b13";}

thanks for your time

 

index.php 

 

<html>
<body>


<form action="insert3.php" method="post">
Firstname: <input type="text" name="firstname[]"> <br>
Firstname 2: <input type="text" name="firstname[]"> <br>
Firstname 3: <input type="text" name="firstname[]"> <br>
Firstname 4: <input type="text" name="firstname[]"> <br>
<input type="submit">
</form>


</body>
</html>

insert.php 

 

<?php
$con=mysqli_connect("localhost","inputmultiplicad","inputmultiplicado","inputmultiplicado");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
  
  


  
  


$data= $_POST['firstname'];
$values= serialize($data);
  
$sql="INSERT INTO input_field (firstname)
VALUES
('$values')";




if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }
echo "1 record added";


mysqli_close($con);
?>

 

Link to comment
Share on other sites

You're using serialize() on the data before you store it in the database, so I'm guessing you would use unserialize() on the data after you extract it. But, note that storing data in that manner will severely limit what you can do with it.

Link to comment
Share on other sites

Psycho i see what you mean. I dont like serialized storage.

 

Doing what you told me the data storage with differents ID, so how could i read/see the exactly info when i read it?

 

I mean, if im doing a form to register a user info with: name, car model 1 (now with jquery i add dinamically the x numbers of different cars that the user have) if they record it with different ID

 

i have change the code to this :

 

index.php

 

<html>
<head>
<script src="http://code.jquery.com/jquery-2.0.3.min.js"></script><script type="text/javascript" src="//code.jquery.com/jquery-latest.js"></script>




</head>
<body>
<div class="my-form">
    <form role="form" method="post" action="insert3.php">
        <p class="text-box">
            <label for="box1">Box <span class="box-number">1</span></label>
            <input type="text" name="firstname[]" value="" id="box1" />
            <button type="button" class="add-box" href="#">+</button>
        </p>
        <p><input type="submit" value="Submit" /></p>
    </form>
</div>
<script>
jQuery(document).ready(function($){
    $('.my-form .add-box').click(function(){
        var n = $('.text-box').length + 1;
        var box_html = $('<p class="text-box"><label for="box' + n + '">Box <span class="box-number">' + n + '</span></label> <input type="text" name="firstname[]" value="" id="box' + n + '" /> <button type="button" class="remove-box" href="#">-</button></p>');
        box_html.hide();
        $('.my-form p.text-box:last').after(box_html);
        box_html.fadeIn('slow');
        return false;
    });
});




$('.my-form').on('click', '.remove-box', function(){
    $(this).parent().css( 'background-color', '#FF6C6C' );
    $(this).parent().fadeOut("slow", function() {
        $(this).remove();
        $('.box-number').each(function(index){
            $(this).text( index + 1 );
        });
    });
    return false;
});
</script>
</body>
</html>

insert3.php (your code, psycho)

<?php
//Run array_map() with trim to trim all the values
//Use array_filter() to remove empty values
$firstnames = array_filter(array_map('trim', $_POST['firstname']));


if(!count($firstnames))
{
    echo "No data to insert";
}
else
{
    //Connect to DB
    $con=mysqli_connect("localhost","inputmultiplicad","inputmultiplicado","inputmultiplicado");
    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    //Use array_map() with mysql_real_escape_string() to sanitize values
    $firstnamesSQL = array_map('mysql_real_escape_string', $firstnames);
    //Implode the values into a string for the query
    $values = "('" . implode("'), ('", $firstnames) . "')";
    $sql = "INSERT INTO input_field (firstname) VALUES {$values}";
    if (!mysqli_query($con, $sql))
    {
        die('Error: ' . mysqli_error($con));
    }
    //Output success message
    echo count($firstnames) . " record(s) added";
    //Close DB connection
    mysqli_close($con);


}
?>

thank you for trying to help man, i appreciate (excuse my english)

 

 

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.