Jump to content

unasemana

Members
  • Posts

    12
  • Joined

  • Last visited

unasemana's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. just solve it, it was something really stupid jus change this line in the update.php $select_tbl=mysql_query("select * from input_field"); to: $select_tbl=mysql_query("select * from input_field WHERE id='$id'");
  2. the file list.php shows the content of the table, is a table that shows a list of ID, names, and secondnames, that list in every line has a EDIT button and a delete button. When you click Edit it will take you to update.php, this file shows the selected ID, an input field type text with the selected apellido and if you want to chage it you change it. But firstname is an array[] that is created with jquery so people could insert more than one. this field should show the exact names but it shows the last on the table and not the selected. Thats the problem im having. the other fields are shown correctly. excuse my english
  3. Hi, im having problems trying to show the selected items array. I mean, i have a list that shows a table with ID, Name,Apellido. (name is the one with dinamic array []. When i se the table everthing is allright, but when i click the edit button, i get right id, the right apellido and name gets the last array inserted. If i click to edit ID number 5 and i have 15, it will show me the 15 instead of number 5 (the selected one). could anyone find what im doing wrong? thanks this are the files im using list.php <?php $host="localhost"; // Host name $username="inputmultiplicad"; // Mysql username $password="inputmultiplicado"; // Mysql password $db_name="inputmultiplicado"; // Database name $tbl_name="input_field"; // Table name // Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $sql="SELECT * FROM $tbl_name"; $result=mysql_query($sql); ?> <table width="400" border="0" cellspacing="1" cellpadding="0"> <tr> <td> <table width="400" border="1" cellspacing="0" cellpadding="3"> <tr> <td colspan="4"><strong>List data from mysql </strong> </td> </tr> <tr> <td align="center"><strong>ID</strong></td> <td align="center"><strong>Name</strong></td> <td align="center"><strong><a href="agregar_cliente.php">Add+</a></strong></td> </tr> <?php while($rows=mysql_fetch_array($result)){ ?> <tr> <td><? echo $rows['id']; ?></td> <td><? echo $rows['firstname']; ?></td> <td align="center"><a href="ver_update.php?id=<? echo $rows['id']; ?>">Editar</a></td> <td bgcolor="#FFFFFF"><a href="delete_ac.php?id=<? echo $rows['id']; ?>">delete</a></td> </tr> <?php } ?> </table> </td> </tr> </table> <?php mysql_close(); ?> and update.php <!DOCTYPE html> <?php $host="localhost"; // Host name $username="inputmultiplicad"; // Mysql username $password="inputmultiplicado"; // Mysql password $db_name="inputmultiplicado"; // Database name $tbl_name="input_field"; // Table name // Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $id=$_GET['id']; $first_name=$_GET['first_name']; // Retrieve data from database $sql="SELECT * FROM $tbl_name WHERE id='$id'"; $result=mysql_query($sql); $rows=mysql_fetch_array($result); // Retrieve data from database to implode/explode $select_tbl=mysql_query("select * from input_field"); while($fetch=mysql_fetch_object($select_tbl)) { echo $id; $r=$fetch->firstname; $i=explode(",",$r); } ?> <html> <head> <link rel="stylesheet" type="text/css" href="css/estilos.css" media="screen" /> <script src="http://code.jquery.com/jquery-2.0.3.min.js"></script> <link href='http://fonts.googleapis.com/css?family=Oxygen' rel='stylesheet' type='text/css'> <meta charset="utf-8"> <title>JS Bin</title> </head> <body> <div id="main contenedor"> <header> <section class="logo">LOGO</section> <!--logo--> </header> <div id="contenedor"> <div class="insertar_formulario"> <h3>Editar Cliente</h3> <h3>id</h3> <? echo $rows['id']; ?> <form name="form1" method="post" action="update_ac.php"> <section class="nombre"><h2>Nombre</h2> <? for($x = 0; $x < count($i); $x++ ) { echo "<input type='text' name='firstname[]' value='$i[$x]'><input type='button' value='+'/><br />"; }; ?> </section> <section class="apellido"><h2>Apellido</h2> <input type='text' name='apellido[]' value='<? echo $rows['apellido']; ?>'/> </section> <section class="enviar"> <input type="submit" name="Submit" value="Submit"> <input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>"> </section> </form> </div> </div><!--contenedor--> </div> <!--main contenedor--> </body> </html> <?php // close connection mysql_close(); ?>
  4. 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)
  5. 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); ?>
  6. thank you all for your help will read about prepared statements thanks again
  7. hi, thanks for your response, I made it: this is working. Why do you dont like serialize sKunBad? What do you mean by : "prepared statement and insert each record individually" ? Pyscho thanks for your asnwers <?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); ?>
  8. will read it and try to understand what you wrote...thanks for your replay
  9. im working on a project but first i would to understand one thing. i have 2 input type text field with name="firstname[]" as an array (in the example im working with no jquery but it will be generated dinamically with it) and cant make it to mysql. here is what i have: index.php <html> <body> <form action="insert.php" method="post"> Firstname: <input type="text" name="firstname[]"> <br> Firstname 2: <input type="text" name="firstname[]"> <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(); } $sql="INSERT INTO input_field (firstname) VALUES ('$_POST[firstname]')"; if (!mysqli_query($con,$sql)) { die('Error: ' . mysqli_error($con)); } echo "1 record added"; mysqli_close($con); ?> the question is: how can i send the firstname[] array to the mysql database?
  10. hi, i have this update.php file that read my clients db. everything is fine but, i cant make it to remember the list of the form, so it gets the first one again, not a selected one. im triying something like this but its not working : any ideas? thanks for your time. <select name="modelo" id= "modelo"> <option value = "Sentra" <?php compare($row->role, 'sentra'); ?>>Sentra</option> <option value = "Corolla" <?php compare ($row->role, 'corolla'); ?>>Corolla</option> <option value = "Chavez" <?php compare($row->role, 'chavez'); ?>>Chavez</option> </select> update.php (full) <?php $host="localhost"; // Host name $username="root"; // Mysql username $password="root"; // Mysql password $db_name="rmm_invoice"; // Database name $tbl_name="clientes"; // Table name // Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // get value of id that sent from address bar $id=$_GET['id']; // Retrieve data from database $sql="SELECT * FROM $tbl_name WHERE id='$id'"; $result=mysql_query($sql); $rows=mysql_fetch_array($result); ?> <table width="100%" border="0" cellspacing="1" cellpadding="0"> <tr> <form name="form1" method="post" action="update_ac.php"> <td> <table width="100%" border="0" cellspacing="1" cellpadding="0"> <tr> <td> </td> <td colspan="3"><strong>Update data in mysql</strong> </td> </tr> <tr> <td align="center"> </td> <td align="center"> </td> <td align="center"> </td> <td align="center"> </td> </tr> <tr> <td align="center"> </td> <td align="center"><strong>Id</strong></td> <td align="center"><strong>Nombre</strong></td> <td align="center"><strong>Apellido</strong></td> <td align="center"><strong>Email</strong></td> <td align="center"><strong>Telf_hab</strong></td> <td align="center"><strong>Telf_cel</strong></td> <td align="center"><strong>Direccion</strong></td> <td align="center"><strong>Modelo</strong></td> </tr> <tr> <td> </td> <td align="center"> <? echo $rows['id']; ?> </td> <td align="center"> <input name="nombre" type="text" id="nombre" value="<? echo $rows['nombre']; ?>"> </td> <td align="center"> <input name="apellido" type="text" id="apellido" value="<? echo $rows['apellido']; ?>" size="15"> </td> <td> <input name="email" type="text" id="email" value="<? echo $rows['email']; ?>" size="15"> </td> <td align="center"> <input name="telf_hab" type="text" id="telf_hab" value="<? echo $rows['telf_hab']; ?>"> </td> <td align="center"> <input name="telf_cel" type="text" id="telf_cel" value="<? echo $rows['telf_cel']; ?>" size="15"> </td> <td> <input name="direccion" type="text" id="direccion" value="<? echo $rows['direccion']; ?>" size="15"> </td> </tr> <tr> <select name="modelo" id= "modelo"> <option value = "Sentra" <?php compare($row->role, 'sentra'); ?>>Sentra</option> <option value = "Corolla" <?php compare ($row->role, 'corolla'); ?>>Corolla</option> <option value = "Chavez" <?php compare($row->role, 'chavez'); ?>>Chavez</option> </select> </tr> <tr> <td> </td> <td> <input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>"> </td> <td align="center"> <input type="submit" name="Submit" value="Guardar"> </td> <td> </td> </tr> </table> </td> </form> </tr> </table> <?php // close connection mysql_close(); ?>
  11. well i just figure it out , it's cause on the update_ac.php it's not actually $_POST'ing the variables, so adding this will work PHP Code: $name = $_POST['name']; $lastname = $_POST['lastname']; $email = $_POST['email']; $id = $_POST['id']; Thanks for your time
  12. hi, im doing this tutorial http://www.phpeasystep.com/mysql/9.html about updating a mysql table. It seems to work but it doesn't update, its like nothing happen. its about 3 files : (working on local with MAMP) list_records.php <?php $host="localhost"; // Host name $username="root"; // Mysql username $password="root"; // Mysql password $db_name="test"; // Database name $tbl_name="test_mysql"; // Table name mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $sql="SELECT * FROM $tbl_name"; $result=mysql_query($sql); ?> <table width="400" border="0" cellspacing="1" cellpadding="0"> <tr> <td> <table width="400" border="1" cellspacing="0" cellpadding="3"> <tr> <td colspan="4"><strong>List data from mysql, <a href='insert.php'>Add New</a> </strong> </td> </tr> <tr> <td align="center"><strong>id</strong></td> <td align="center"><strong>Name</strong></td> <td align="center"><strong>Lastname</strong></td> <td align="center"><strong>Email</strong></td> <td align="center"><strong>Update</strong></td> </tr> <?php while($rows=mysql_fetch_array($result)){ ?> <tr> <td><? echo $rows['id']; ?></td> <td><? echo $rows['name']; ?></td> <td><? echo $rows['lastname']; ?></td> <td><? echo $rows['email']; ?></td> <td align="center"><a href="update.php?id=<? echo $rows['id']; ?>">update</a></td> <td bgcolor="#FFFFFF"><a href="delete_ac.php?id=<? echo $rows['id']; ?>">delete</a></td> </tr> <?php } ?> </table> </td> </tr> </table> <?php mysql_close(); ?> update.php ############### Code <?php $host="localhost"; // Host name $username="root"; // Mysql username $password="root"; // Mysql password $db_name="test"; // Database name $tbl_name="test_mysql"; // Table name // Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // get value of id that sent from address bar $id=$_GET['id']; // Retrieve data from database $sql="SELECT * FROM $tbl_name WHERE id='$id'"; $result=mysql_query($sql); $rows=mysql_fetch_array($result); ?> <table width="400" border="0" cellspacing="1" cellpadding="0"> <tr> <form name="form1" method="post" action="update_ac.php"> <td> <table width="100%" border="0" cellspacing="1" cellpadding="0"> <tr> <td> </td> <td colspan="3"><strong>Update data in mysql</strong> </td> </tr> <tr> <td align="center"> </td> <td align="center"> </td> <td align="center"> </td> <td align="center"> </td> </tr> <tr> <td align="center"> </td> <td align="center"><strong>Id</strong></td> <td align="center"><strong>Name</strong></td> <td align="center"><strong>Lastname</strong></td> <td align="center"><strong>Email</strong></td> </tr> <tr> <td> </td> <td align="center"> <? echo $rows['id']; ?> </td> <td align="center"> <input name="name" type="text" id="name" value="<? echo $rows['name']; ?>"> </td> <td align="center"> <input name="lastname" type="text" id="lastname" value="<? echo $rows['lastname']; ?>" size="15"> </td> <td> <input name="email" type="text" id="email" value="<? echo $rows['email']; ?>" size="15"> </td> </tr> <tr> <td> </td> <td> <input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>"> </td> <td align="center"> <input type="submit" name="Submit" value="Submit"> </td> <td> </td> </tr> </table> </td> </form> </tr> </table> <?php // close connection mysql_close(); ?> and update_ac.php <?php $host="localhost"; // Host name $username="root"; // Mysql username $password="root"; // Mysql password $db_name="test"; // Database name $tbl_name="test_mysql"; // Table name // Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // update data in mysql database $sql="UPDATE $tbl_name SET name='$name', lastname='$lastname', email='$email' WHERE id='$id'"; $result=mysql_query($sql); // if successfully updated. if($result){ echo "Successful"; echo "<BR>"; echo "<a href='list_records.php'>View result</a>"; } else { echo "ERROR"; } ?> can anyone see what could i be doing wrong even that im following the tutorial? thanks for your time.
×
×
  • 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.