I am struggling with some PHP / MySQL code. I'm reading from table 1, changing some fields and writing on another table, if someone joins and if one of the array values is zero, then I would like to insert the null in the database (zero values in the field Is allowed). Looks like something:
$ result = mysql_query ("select * from mytable"); while {$ row = mysql_fetch_assoc ($ result) {mysql_query ("table2 (f1, f2) values Enter ('{$ row [' string_field ']}', {$ row ['null_field']})} Not every line has a zero value and more in my query Field and 2 columns that can not be taped or not
This is an example where are ready The statement really protects you from some problems. In MySQL, Value for inserting space, you must specify it at INSERT time or leave the field for which additional branches are required: table 2 (F1, F2) values (INSERT '' string value), NULL); However, if you want to include a value in that field, you now have to branch out your code to add a single code:
Insert Table 2 (F1, F2) value ('string value', 'string value'); Ready statement automatically does this for you. Address them as string (0) "" and null and your query properly: $ stmt = $ mysqli- & Gt; Prepare ("Table 2 (F1, F2) INSERT VALUES (?,?)"); $ Stmt- & gt; Dam_param ('s', $ field1, $ field2); $ Field1 = "string value"; $ Field2 = faucet; $ Stmt- & gt; Executed (); This saves your field for you, ensures that you do not forget to pair a parameter. There is no reason to be with the mysql extension, use mysqli and instead. You will save yourself a world of pain.
Comments
Post a Comment